in reply to Re^4: Getting srand's seed
in thread Getting srand's seed
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Getting srand's seed
by fangly (Initiate) on Oct 10, 2008 at 18:41 UTC |