# Random Number Generator Seed Mistakes and How to Seed an RNG

### Looking at randomization software, what can go wrong, and how to fix it.

Join the DZone community and get the full member experience.

Join For Free## long run or broken software?

i got a call one time to take a look at randomization software that wasn’t randomizing. my first thought was that the software was working as designed, and that the users were just seeing a long run. long sequences of the same assignment are
more likely than you think
. you might argue, for example, that the chances of flipping five heads in a row would be (1/2)
^{
5
}
= 1/32, but that underestimates the probability because a run could start at any time. the chances that the
*
first
*
five flips are heads would indeed be 1/32. but the probability of seeing five heads in a row any time during a series of flips is higher.

most of the times that i’ve been asked to look at randomization software that “couldn’t be right,” the software was fine. but in this case, there wasn’t simply a long run of random results that happened to be equal. the results were truly constant. at least for some users. some users would get different results from time to time, but others would get the same result every single time.

the problem turned out to be how the software set the seed in its random number generator. when the program started up it asked the user “enter a number.” no indication of what kind of number or for what purpose. this number, unbeknownst to the user, was being used as the random number generator seed. some users would enter the same number every time, and get the same randomization result, every time. others would use more whimsy in selecting numbers and get varied output.

how do you seed a random number generator in a situation like this? a better solution would be to seed the generator with the current time, though that has drawbacks too. i plan to write about that in my next post.

## seeding many independent processes

a more subtle problem i’ve seen with random number generator seeding is spawning multiple processes that each generate random numbers. in a well-intentioned attempt to give each process a unique seed, the developers ended up virtually assuring that many of the processes would have exactly the same seed.

if you parallelize a huge simulation by spreading out multiple copies, but two of the processes use the same seed, then their results will be identical. throwing out the redundant simulation would reduce your number of samples, but not noticing and keeping the redundant output would be worse because it would cause you to underestimate the amount of variation.

to avoid duplicate seeds, the developers used a random number generator to assign the rng seeds for each process. sounds reasonable. randomly assigned rng seeds give you even more random goodness. except they don’t.

the developers had run into a variation on the famous birthday problem. in a room of 23 people, there’s a 50% chance that two people share the same birthday. and with 50 people, the chances go up to 97%. it’s not
*
certain
*
that two people will have the same birthday until you have 367 people in the room, but the chances approach 1 faster than you might think.

applying the analog of the birthday problem to the rng seeds explains why the project was launching processes with the same seed. suppose you seed each process with an unsigned 16-bit integer. that means there are 65,536 possible seeds. now suppose you launch 1,000 processes. with 65 times as many possible seeds as processes, surely every process should get its own seed, right? not at all. there’s a 99.95% chance that two processes will have the same seed.

in this case it would have been better to seed each process with
*
sequential
*
seeds: give the first process seed 1, the second seed 2, etc. the
*
seeds
*
don’t have to be random; they just have to be unique. if you’re using a good random number generator, the outputs of 1,000 processes seeded with 1, 2, 3, …, 1000 will be independent.

## help with randomization

if you need help with randomization , please contact me. i can help you avoid subtle errors and have randomization procedures that will stand up to scrutiny.

Published at DZone with permission of John Cook, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Comments