Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: session keys: how far to take it

by BrowserUk (Patriarch)
on Dec 28, 2004 at 12:22 UTC ( [id://417750]=note: print w/replies, xml ) Need Help??


in reply to session keys: how far to take it

This will generate a million unique 128-bit random numbers in around 20 seconds, and happily goes up to 10 million (and I suspect very, very much higher, but I ran out of memory) without duplication.

If you want them hexified use unpack as in the commented out print statement.

#! perl -slw use strict; use Time::HiRes qw[ time ]; use Math::Random::MT qw[ rand srand ]; $| = 1; our $MAX ||= 1000000; srand( time ); my %hash; for ( 1 .. $MAX ) { my $rand = pack 'N4', map{ rand 0xffffffff } 1..4; die "Got duplicate after $_ attempts" if exists $hash{ $rand }; $hash{ $rand } = (); # print unpack 'H*', $rand; printf "\r$_\t" unless $_ % 1000; } __END__ [12:20:07.29] P:\test>417743 -MAX=1000000 1000000

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

Replies are listed 'Best First'.
Re^2: session keys: how far to take it
by BrowserUk (Patriarch) on Dec 28, 2004 at 23:46 UTC

    If I understand the math at all (and it is quite possible I don't!), the odds against the above producing a duplicated ID at any one choice are something like: 0.000000000000000000054

    Which, when compared with your odds of being hit by falling space debris: 0.0000000000000166

    Or getting struck by lightning: 0.000001763

    Or that you would be one of those caught up in the recent tsunami: 0.000008333

    Seem pretty good odds to me.


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-23 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found