File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes General Computing and the fly likes Random collisions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » General Computing
Bookmark "Random collisions" Watch "Random collisions" New topic

Random collisions

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

google for 'birthday paradox'
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

Joined: Jan 29, 2003
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

Joined: Oct 14, 2005
Posts: 19973

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.
I agree. Here's the link:
subject: Random collisions
It's not a secret anymore!