Random numbers with specified seeds so can reproduce the same result next time?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By chanon
:warning: Old Version Published before Godot 3 was released.

Is there a Class/methods to have multiple independent random number generators each initialized with separate seeds?

This is useful for example when needing to use random numbers but be able to reproduce the same result next time. (Ex. ‘world seed’ for a prodcedurally generated world)

:bust_in_silhouette: Reply From: avencherus
rand_seed(seed)

Oh, I see … so I pass the seed that is returned back back into rand_seed to get the next random number and so on?

What is the range and type of the returned random number?

chanon | 2016-11-26 17:32

OK, so I’ve tried it and the value returned is always a largish integer. Since I don’t know the possible range of the integer, then I can’t use it.

chanon | 2016-11-27 15:36

Use seed(int value) to set the seed and then use rand(float from, float to) so that you can get a specific range. If you need it to be an integer, use round(float value) on the value that rand returned.

If that way doesn’t work, you can take the large integer value you were getting and use fmod(random_num, max_value_possible). This will make it so that the number returned cannot be larger than you want.

Frank Nielson | 2016-11-28 06:41