http://qs321.pair.com?node_id=513829


in reply to Re: Generate a session ID
in thread Generate a session ID

I'm not sure what the mathematical probability is for picking duplicates from a pool 2^15 values, but in practice, it produces very, very few unique values before it repeats itself:
This is an application of the Birthday Paradox, which isn't really a paradox, just counter-intuitive. Here's a little code that shows how many you have to pick before your chances are greater than 50% of getting a dupe. The number is quite a bit lower than you would think.
my $prod = 1; foreach my $num (1..2**15) { printf("%5d => %0.6f\n", $num, $prod); die "Threshold reached\n"if $prod <= 0.5; $prod *= (2**15 - $num)/2**15; }
Update: In looking back, it may be a little unclear what the program above produces. It prints a list of pairs in the form of "x => y". 'x' is the number of IDs that you've generated, 'y' is the probability that you've chosen a unique one. I also had a typo (one too many '}'s) and a slight logic error (the third line used to be the first).

thor

The only easy day was yesterday