How often do we have to call randomize()?

22_hm_gdt
godot
heist-meisters

#1

I would have assumed that we only need to call randomize() once to seed the random number generator. For example, maybe just call it in the Global _ready function and then we’re good to go for the rest of the game.

Instead, we seem to be calling randomize before every random number we generate. Is this necessary?


#2

The Godot Engine uses a PCG Family implementation for generating random numbers. It is not required to reseed the random number generator to continue to get “random” numbers. Reseeding isn’t going to hurt anything but there are reasons why people do. The default implementation for Python is Mersenne Twister. Mersenne Twister is considered predictable by some, with claims that it is completely predictable after 624 outputs. Reseeding can mitigate this kind of predictability, but I don’t think PCG suffers from this kind of problem and so GDScript is perhaps better than Python at generating random numbers.


#3

Do you know if there is really no performance penalty whatsoever when you change the seed so often?

Also, for people like me who wondered if the PCG familiy of pseudo random number generators is cryptographically secure:
It’s not!
See the very helpful table on http://www.pcg-random.org for more info.


#4

Yes, it would be sufficient to call randomize only once in a global script, like @adamadair already pointed out.

Personally I like to keep stuff contained to where it is relevant so I put it into the _ready function of CombinationGenerator