Check the rand function used by your version of Perl. Specifically, check for the number of randbits it is configured for:

P:\test>Perl -v This is perl, v5.8.7 built for MSWin32-x86-multi-thread (with 7 registered patches, see perl -V for more detail) ... P:\test>Perl -V:randbits randbits='15';

Which means that with AS perl, this snippet will only ever produce 32768 unique ids:

#! perl -slw use strict; my %cache; $cache{ sprintf("%0.8x",rand()*0xffffffff) } = undef for 1 .. 1_000_00 +0; printf "1 million attempts produced %d keys\n", scalar keys %cache; printf "between %s and %s\n", (sort keys %cache)[ 0, -1 ]; __END__ P:\test>junk 1 million attempts produced 32768 keys between 00000000 and fffdffff

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:

#! perl -slw use strict; my %cache; my $u = sprintf("%0.8x",rand()*0xffffffff); until( exists $cache{ $u } ) { $cache{ $u } = undef; $u = sprintf("%0.8x",rand()*0xffffffff); } printf "Produced %d unique keys before duplicating\n", scalar keys %ca +che; printf "between %s and %s\n", (sort keys %cache)[ 0, -1 ]; __END__ P:\test>junk Produced 454 unique keys before duplicating between 00bdffff and ff89ffff P:\test>junk Produced 273 unique keys before duplicating between 013dffff and feb5ffff P:\test>junk Produced 84 unique keys before duplicating between 0183ffff and fe5bffff P:\test>junk Produced 94 unique keys before duplicating between 001bffff and fbc1ffff

You can extend the range almost linearly by multiplying two calls to rand:

#! perl -slw use strict; my %cache; $cache{ sprintf( "%0.8x", rand() * rand() * 0xffffffff ) } = undef for + 1 .. 1_000_000; printf "1 million attempts produced %d keys\n", scalar keys %cache; printf "between %s and %s\n", (sort keys %cache)[ 0, -1 ]; __END__ P:\test>junk 1 million attempts produced 994758 keys between 00000000 and ffae0137

but even then, the number of uniques you will get before repeats is dismally low:

Produced 17891 unique keys before duplicating between 00000000 and fba0116f P:\test>junk Produced 1957 unique keys before duplicating between 0008e633 and fb575407 P:\test>junk Produced 30229 unique keys before duplicating between 00000000 and fdeaed07 P:\test>junk Produced 10830 unique keys before duplicating between 0000eedf and ff441eff P:\test>junk Produced 15537 unique keys before duplicating between 00000000 and fe068a77

All in all, a better session id generator than this would be strongly advisable.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Generate a session ID by BrowserUk
in thread Generate a session ID by Spidy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.