Random Perl Content

Ruud H.G. van Tol rvtol at isolution.nl
Sun May 19 01:58:25 BST 2013

On 19/05/2013 02:26, Ruud H.G. van Tol wrote:
> On 18/05/2013 23:29, Abigail wrote:
>> On Sat, May 18, 2013 at 01:53:33PM -0700, Randy J. Ray wrote:
>>> (It's a pun, see, because I'm going to be asking about random number
>>> generators... get it? Get it...?)
>>> (Short, TL;DR summary: I'm looking for a pRNG that can have multiple
>>> instances at once that don't affect each others' stream of numbers. Does
>>> Math::Random::MT meet that criteria?)
>>> I considered posing this to Perlmonks, but I just don't frequent that
>>> site like I used to. And there are enough Large Perl Brains on this list
>>> that I should get as good an answer, if not better.
>>> I am looking for a pRNG (pseudo-Random Number Generator) module. But I
>>> have an unusual requirement-- I need to be able to instantiate multiple
>>> *independent* generators, that can take seeds for the sake of
>>> reproducible results. Let me explain...
>>> My current (paying) job is writing frameworks for automated QA of my
>>> company's software (mainly the operating system that runs our storage
>>> servers). We are working on a new approach to how we structure some of
>>> our tests, and that happens to involve the potential of using some
>>> randomization to select different paths over a directed graph. But, this
>>> being QA-oriented, even if something runs with a degree of randomness to
>>> it, it needs to be reproducible at a later time. So, no problem, just
>>> create a seed for srand() and log that seed, and also provide users a
>>> way to specify a seed at the start. Then you can just re-use the seed
>>> and reproduce your results. Right? Well, not exactly...
>>> Other libraries I use, developed by other teams within our company,
>>> might also have some randomization in them (like generating random names
>>> for disk volumes, randomizing data generation for traffic testing,
>>> etc.). Not to mention that we use no small number of CPAN modules, some
>>> of which might use rand() as well. I could seed Perl's RNG with a
>>> specific seed, but if that run has to try twice to generate a unique
>>> file name, instead of one or thrice, that will affect the random numbers
>>> *my* code gets.
>>> So my thought was that sure there's a RNG module out there that is OO,
>>> and encapsulates the seed and all other internal elements of the
>>> generation process. One that I can instantiate multiple instances of,
>>> and have them generate streams of random numbers that are independent of
>>> each other. The closest I've found is Math::Random::MT, which implements
>>> the Mersenne Twister pRNG. But I can't immediately tell from the docs
>>> whether the object instances it creates are truly independent of each
>>> other or not.
>>> So, is this something anyone else here has dealt with? Are there modules
>>> I just haven't stumbled upon yet, that would do this for me? Any help
>>> greatly appreciated, as always.
>> I'm not aware of any Perl modules that provide this functionality,
>> but on my Linux box, there are the C functions erand48, nrand48
>> and jrand48. It would be fairly trivial to call those functions
>> using a few simple lines of XS.
> Math::Rand48 looks promising.

perl -Mstrict -MMath::Rand48=nrand48 -wle '
   sub mk_rand { my $seed = shift; return sub {nrand48($seed)} }
   my $rand1 = mk_rand();  # same as mk_rand(0)
   my $rand2 = mk_rand(42);
   print "1:", join ",", map $rand1->(), 1 .. $ARGV[0];
   print "2:", join ",", map $rand2->(), 1 .. $ARGV[1];
   print "1:", join ",", map $rand1->(), 1 .. $ARGV[2];
   print "2:", join ",", map $rand2->(), 1 .. $ARGV[3];
' 2 1 2 1

perl -Mstrict -MMath::Rand48=nrand48 -wle '
' 1 2 1 2


More information about the london.pm mailing list