Posted on 2007-04-09 14:51:22-07 by jdhedden in response to 4766
Re: Problem with acquiring random seeds
While MRMA does try to get a full seed array from random sources, it is not alway guaranteed to do so. If a full seed is not acquired, a warning is issued, and the values in the seed are repeated to fill up the PRNG. In such a case, get_seed returns a full array with some duplicate values. For instance, in the case when no sources available, MRMA will use the PID and time as a partial seed, and then repeat them over and over until the PRNG is fully seeded. get_seed will then return (PID, time, PID, time, PID, time, ...). This phenonomenon accounts for the repeated values in your table.

For this reason, I don't recommend saving seeds as a source of random numbers.

That being said, you can ward against partial seeds by trapping warnings when doing srand, and then skipping the seed:
use Math::Random::MT::Auto ':!auto'; my @WARN; $SIG{'__WARN__'} = sub { push(@WARN, @_); }; my $prng = Math::Random::MT::Auto(); for (1..50) { # This will exhaust your daily quota $prng->srand('random_org'); if (@WARN) { print('Skipping seed: ', @WARN); undef(@WARN); } else { my @seed = $prng->get_seed(); # Store seed in database } sleep(10); }
You also asked about using /dev/urandom. This device draws from /dev/random until such time (if ever) that /dev/random is exhausted. Then it falls back to a PRNG to fill the rest of the request. So using /dev/urandom is a good idea if you're not using it too frequently.

If you want to guarantee a fully random seed, however, then use /dev/random. If you need a full seed, then trap warnings as per the above and using:
$prng->srand('/dev/random/');
Is MRMA better than rand() even if using just on 32-bit seed? Yes, because MRMA can produce a longer sequence before repeating. (I think I give some idea on this in the POD, but maybe not.)
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.