Pseduo random number sequence generators (which is what we are really talking about) are tricky things and are subject to much tricky maths that I don't understand. However there are various quality attributes that characterise such generators and default rand generators often exhibit among the worst results for those qualities.

One simple quality is the number of bits used by the generator. Perl tends to use the rand provided by the runtime library of the C compiler used to build it. ActiveState's Perl build for example uses only 15 bits! You can find out how many bits you Perl build uses by:

use Config; print $Config{randbits};

In fact the seeding snippet I gave earlier could be rewritten using that information as $seed = int(rand(2**$Config{randbits} - 1)); (although that may produce odd results if $Config{randbits} == $Config{longsize} * 8). Note that 2**2**$Config{randbits} is generally the maximum cycle length for rand.

Another area where rand is often poor is correlation between successive values. This may be a more important consideration where you are generating numbers for modelling or statistical applications. There is a pile of stuff on the internet about these sorts of applications and suitable generators. You could start at Pseudorandom_number_generator.

For your application I strongly recommend that you use Math::Random::MT for your actual generator. It is a robust generator that is generally quick enough and has a very long cycle period. It is also very easy to use. Just put:

use Math::Random::MT qw(srand rand);

and you are in business - the MT generator replaces Perl's built in rand seamlessly.


Perl reduces RSI - it saves typing

In reply to Re^5: Getting srand's seed by GrandFather
in thread Getting srand's seed by fangly

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.