- Home /
Is Random.Range really uniform?
I have a script randomly generating instances of different game objects. It uses Random.Range to decide which one to instantiate, but I'm not entirely convinced that it's returning a uniform distribution. I've been trying it with just two objects and have it count how many of each it spawns, and it seems to favor one object over the other. I just let it run, generating one random object per frame for several minutes, and the gap between the two numbers is now past 100 and steadily climbing (not quickly, but steadily). Every time I run it, I set the seed to be (int)Time.time, but it always creates more of the first object than the second one, no matter how many times I run it. Is there some trick to getting the distribution to be more uniform? Thanks a lot!
There is a difference between random and uniform...while most humans would think the more uniform a distribution is, the more random it is, this is not true. If you want a uniform distribution, you would have to impose limits on the RNG to prevent bunched up results.
Quite simply do NOT set the seed, leave it alone.
So, that's the answer. If that does not work, you have an unrelated bug, or, as Pfifik suggest you're just not used to seeing real random numbers.
If you're setting the seed to Time.time in Start or Awake, then it's always being set to 0, which explains why you get the same results every time.
Yes, I understand not every random distribution will be completely uniform; maybe I used the ter$$anonymous$$ology wrong, but I just meant that a large amount of random numbers should be more or less evenly distributed across the range in question (as opposed to, say, a normal distribution). And it did not appear to be evenly distributed, because like I said, the gap between the number of each value chosen kept growing. But anyway, removing the line where I set the seed seems to have helped. I'm not sure why ANY seed would have caused the behavior I was seeing though... most likely I'm still a bit ignorant about how random numbers actually work. ^^; Thanks for the help!
I'm not sure why ANY seed would have caused the behavior I was seeing though...
The whole point of setting the seed is to get the same results every time.
Answer by Eric5h5 · Aug 30, 2012 at 12:42 AM
There's no reason to set the seed, unless you specifically want a repeatable sequence. Unity uses Marsaglia's Xorshift 128 algorithm, which is high quality. This code demonstrates that there is no particular bias:
var x = 0;
var y = 0;
for (var i = 0; i < 1000000; i++) {
if (Random.Range(0, 2)) {
x++;
}
else {
y++;
}
}
Debug.Log (x + " " + y);
If you run it a number of times, you'll find that x is greater than y approximately half the time, and both x and y always end up being fairly close to 500,000.
For some reason when I set the seed at the beginning, the results are NOT uniformly distributed, but when I remove the seed, it works fine. $$anonymous$$ysterious... Anyhow, thanks for the tip.
This doesn't prove uniform distribution. "Normal" distribution would also have same amount values around the average point, but it's not uniform.