Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^2: Generate a session ID

by thor (Priest)
on Dec 03, 2005 at 17:21 UTC ( #513829=note: print w/replies, xml ) Need Help??

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).


The only easy day was yesterday

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://513829]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2020-07-08 14:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found