in reply to How likely is rand() to repeat?
Mathmatically, I think the odds of any given pick being a duplicate of the previous pick are ~1/6.45e+44, which is vanishingly unlikely. Obviously, the more you pick the more likely a duplicate, but given the starting odds, even if you are picking trillions they are still very unlikely.
Something like 1e12*2(*)/6.45e44 ~= 1/3.22e32. (For reference, that about a million times the number of grams in the entire Earth!)
((*) For the Birthday paradox)
Of course, it also depends somewhat upon the validity of your rand(), but even using the notoriously poor rand() built-in to Win32 perl, I didn't get a single dup in 1e7 picks:
undef %h; ++$h{ join'', map $chars[ rand @chars ], 1 .. 25 } for 1 .. 1e7; print scalar keys %h;; 10000000
You could use a better rand, like Math::Random::MT, but it is probably unnecessary unless you are picking trillions. It is also much slower than the built-in on my machine.
Finally, no matter how good your rand(), even with those vanishingly small odds, there are no guarantees that you won't get a duplicate. The odds may be very small against it happening, but it still can happen. You are very, very unlikely to see it happen twice in your lifetime though.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How likely is rand() to repeat?
by desertrat (Sexton) on Mar 08, 2012 at 21:50 UTC | |
by Your Mother (Archbishop) on Mar 08, 2012 at 22:01 UTC | |
by BrowserUk (Patriarch) on Mar 09, 2012 at 11:53 UTC | |
by tye (Sage) on Mar 09, 2012 at 17:51 UTC | |
by BrowserUk (Patriarch) on Mar 09, 2012 at 18:03 UTC |