I still don't quite understand why this was only an issue on windows

The default rand (from the MS CRT) only produces 15-bits of randomness. Ie. int( rand*2**15 ) produces: 0 .. 32767.

If you multiply by a larger factor, there are gaps in the range of values produced. Eg. int( rand() * 65535 ) produces 32767 unique values: 0 .. 65535 step 2.

Hence your problem. Used as is, it will never produce 50,000 unique values.

It can be used to provide a greater range with a little ingenuity. This packs 8 random bytes and unpack as a quad to produce a reasonable 64-bit rand that is still quicker than the MT:

sub myRand{ unpack('Q', pack'C8', map rand()*256, 1 ..8) / 2**64 } undef%hash; say time; ++$hash{ int( 50000*myRand() ) } while keys %has +h < 50000; say time;; 1376006416.04139 1376006418.40272 say ~~keys %hash;; 50000 print sum values %hash;; 543283

Why this sad state persists in the MS CRT is because it seen as better to keep a seriously deficient rand function for trivial purposes and thus force users to seek out/write a better one for their particular serious purposes.

That if they added (say) the MT, then people would either fall foul of or complain that it wasn't good enough for cryptographic purposes. And if they added a cryptographically-secure PRNG, then people would decry it as too slow for Monte-Carlo simulation purposes. Etc.

Take that as you may.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^4: Is rand() really that slow or am I just using it wrong? by BrowserUk
in thread Is rand() really that slow or am I just using it wrong? by adiuva

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.