Reputation: 149
What I need to do:
I can do all 3 steps, no problem. BUT, I need the changed characters in the string to be the same every time I input this particular string. So, for example:
"Hello" should always become "32864"
But as it is now, I always get different results even when using the same seed. As it is now, I define the new RNG, set the seed, call the function that changes the characters. All in ready().
var test_rand = RandomNumberGenerator.new()
test_rand.seed = hash("Simple key")
print(function("Hello", other parameters, test_rand)
print(function("Hello", other parameters, test_rand)
print(function("Hello", other parameters, test_rand)
All 3 results are different. If you are wondering, no, there are no additional unique RNGs inside the function (text = "Hello"):
var return_string = text
var changed_indexes = []
var index
for i in len(text):
while true:
index = test_rand.randi_range(0, len(text)-1)
if index not in changed_indexes:
changed_indexes.append(index)
break
return_string[index] = alphabet[test_rand.randi() % alphabet.size()]
return return_string
Upvotes: 1
Views: 311
Reputation: 40295
When you seed the random number generator, that seed will define the sequence of results you will get.
That sequence will eventually repeat, after you take a large number of randomic values.
Without going into the mathematics behind it, the idea is that the random number generator holds some internal state, and every time you generate a number with it, it also updates the internal state. By pigeonhole principle, the internal state will eventually repeat, and thus the whole sequence will repeat.
And... When you seed the random number generator, you are setting the internal state.
Thus, if you seed your random number generator, and then run your function with it without seeding it again, every time the function run will get a different result (until eventually it repeats), because it is just further down the sequence.
Since you want it to repeat right away, what you would have to do is to seed it again each time. That is, you want to restart the sequence.
I'm not convinced that a random number generator is the tool you want.
It seems you want to:
That sounds like an HMAC to me.
Godot actually offers an HMAC solution under the Crypto
class (Crypto as in Cryptography, not as in NFT).
Example:
Godot 3
var key := "Simple key"
var data := "Hello"
var crypto := Crypto.new()
var digest = crypto.hmac_digest(
HashingContext.HASH_SHA1,
key.to_utf8(),
data.to_utf8()
)
prints(digest)
Godot 4
var key := "Simple key"
var data := "Hello"
var crypto := Crypto.new()
var digest = crypto.hmac_digest(
HashingContext.HASH_SHA1,
key.to_utf8_buffer(),
data.to_utf8_buffer()
)
prints(digest)
Output:
[87, 94, 68, 177, 213, 141, 100, 111, 144, 212, 87, 173, 176, 199, 210, 133, 50, 194, 134, 7]
Or use prints(digest.hex_encode())
:
575e44b1d58d646f90d457adb0c7d28532c28607
You might also be interested in get_string_from_ascii
.
Above I demonstrated HASH_SHA1
, assuming this is not a security concern, that should be fine. You might even further modify it or truncate it to the length you need. Otherwise know that HASH_SHA256
is also supported, and it is recommended cryptographically secure solution, and don't mess with it.
Upvotes: 2