Random Perl Content

Iain C Docherty londonperlmongers at iandocherty.com
Sun May 19 01:06:08 BST 2013

When looking for answers to these questions, I always get out my
 'Microprocessor programming for computer hobbyists (1977)' which is

A linear congruential generator (or generators) is what you need to
generate 'pseudo-random' numbers.

X = (A*X + C) Mod M;

where A, C and M are 'carefully chosen' constants.

try it with  X = (5 * X + 3) MOD 8

it generates the sequence 0, 3, 2, 5, 4, 7, 6, 1 ...

the following rules are used to chose the constants (assuming M is a power
of 2)

1) A MOD 8 = 5
2) When A is written in binary the digits should not show any simple
pattern, avoid large blocks of 0 or 1. (note, in binary rule 1 has the
rightmost (LS) digits of 101)
3) C should be an odd number near

M*(1 - 1/root(3))/2

For M = 2**16 this works out to be 13849, for M=2**32 it is 907633385

so, using these rules you could define a few generators with different
values for A, e.g.

A = 58653 = 1110010100011101B
C = 13849
M = 65536

A = 1001101001011101B
C = 13849
M = 65536



On 18 May 2013 23:21, Dave Hodgkinson <davehodg at gmail.com> wrote:

> Individual random number daemons?
> Sent from my iPhone
> On 18 May 2013, at 21:53, "Randy J. Ray" <rjray at blackperl.com> 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.
> >
> > Randy
> > --
> >
> """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
> > Randy J. Ray      Sunnyvale, CA      http://www.rjray.org
> rjray at blackperl.com
> > twitter.com/rjray
> > Silicon Valley Scale Modelers: http://www.svsm.org

More information about the london.pm mailing list