This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# Random collisions

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
My dad taught statistics and always did the thing about the odds of two people in class having the same birthday. He's gone now so I turn to the math majors here. How do we compute how often to expect a duplicate or repeat in an rng for n digits or a range? I'm looking at 5 digits, 0..99999.

Pat Farrell
Rancher
Posts: 4678
7
It works out that the odds hit 50% quickly, I think 22 people or so, where your naive expectation is that it would take 180.

I worked with a guy who could calculate it in his head.

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
While working with this I read the code for our custom RNG and got scared.

I started looking for ways to compare randomness and settled on even distribution. I made a unit test that for a parameter n generates n**2 values between 1..n and asserts that 99% of the possible numbers are generated within 10% of the ideal distribution. Java Random flies through this for n over a few hundred with no problem.

The custom generator didn't trust the seed algorithm for some reason and resets the seed to the output value every time. I guess because the output value is many orders of magnitude smaller than the original seed, the value converges on repeating cycles pretty quickly. For 1..10000 38 possible values had counts within 1 of each other, the other 9962 occurred zero or one times.

Moral: You are almost certainly not smarter than the library, or, in this case, Donald Knuth.

Paul Clapham
Sheriff
Posts: 20958
31
Stan, I saw the first part of your post in the Today's Posts section and thought to myself "Better remind him what Knuth said about random-number generators". But... I see that's covered off already. Good. Carry on then.