- Home /
Multiple random values in one frame
Hi guys,
Does anyone happen to know how exactly this works on the background and if it is ok to do:
float one;
float two;
float three;
float four;
float five;
void Update()
{
one = Random.value;
two = Random.value;
three = Random.value;
four = Random.value;
five = Random.value;
}
I need to random bunch of variables in one single frame. When I do it like above and print them, they all seem to random different values and all seems ok. But I heard that since the Random is depended on the clock, randoming in one frame may produce same results... This doesn't happen in my test but I would still like to verify if it's ok to leave the code like this.
If this is fine, then what's the rumor about randoming multiple times in one frame?
Thanks!
Depends on how Random was implemented. If your tests show different results than some rumours, forget about them.
I would say it's using the internal system clock or other "don't repeat" algorithms anyway, so the time is always different, even between calls within two lines of code.
Answer by steakpinball · Mar 02, 2015 at 02:38 PM
When first requesting a random value, the Random class creates a new random number generator. This generator needs a seed which is used to generate the first random value. If one is not provided, the current time in milliseconds is used. All subsequent values are are used to generate the number after it. This means generating several random numbers in the same frame is perfectly fine.
Unity blog has an excellent post about random numbers. http://blogs.unity3d.com/2015/01/07/a-primer-on-repeatable-random-numbers/
by "all subsequent values" you mean milliseconds right? Isn't there a chance that one = Random.value; and two = Random.value; will happen sooner than a millisecond apart?
I'm reading the blog right now btw, thank you!
He means, that every new number is also based on the preceding number. $$anonymous$$eaning there's no chance having the same number twice as every number is used for the next one calculated.
Ah I see, makes sense now. Thanks a lot for the help guys!
Your answer
![](https://koobas.hobune.stream/wayback/20220613184753im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to call a user input variable in another script. 1 Answer
Recording and playback of gameplay using events (NOT video) 1 Answer
How do I generate a "random" number from a seed? 0 Answers
How do you get a seed value from UnityEngine.Random.State? 2 Answers
Can't access string in another script 2 Answers