Re: Random Perl Content
On 19 May 2013, at 07:29, Matthew Seaman wrote: > Why not just write each of your streams of random numbers to a file and > then just re-read the file when you need to reuse that sequence? That involves quite a lot more careful management. The approach of logging seeds is quite widely used. In fact MySQL replication sends out the seed in each replica because they do it ass backwards and forgot someone night actually embed random() in their query. James
Re: Random Perl Content
On 18/05/2013 21:53, Randy J. Ray wrote: > 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. Why not just write each of your streams of random numbers to a file and then just re-read the file when you need to reuse that sequence? Matthew -- Dr Matthew J Seaman MA, D.Phil. PGP: http://www.infracaninophile.co.uk/pgpkey JID: matt...@infracaninophile.co.uk signature.asc Description: OpenPGP digital signature
Re: Random Perl Content
Excellent feedback from everyone! While reading the ideas, it occurred to me how simple it would be to test Math::Random::MT (based loosely on Ruud's testing of Rand48). It turns out that you *can* in fact instantiate completely independent RNG's from ::MT. I banged out the following: #!/usr/bin/env perl use strict; use warnings; use autodie qw(open close); use Math::Random::MT; my ($fh, $ground, $noise, $noise1, $noise2); my $iters = shift(@ARGV) || 1; # 1. Baseline. Generate $iters numbers with no other RNG's running. open $fh, '>', 'rng-1'; $ground = Math::Random::MT->new(5); for (1 .. $iters) { print {$fh} $ground->irand, "\n"; } close $fh; undef $ground; # 2. Generate the same list with one other RNG running as well. open $fh, '>', 'rng-2'; $ground = Math::Random::MT->new(5); $noise = Math::Random::MT->new($$); for (1 .. $iters) { $noise->rand(); print {$fh} $ground->irand, "\n"; } close $fh; undef $ground; undef $noise; # 3. Generate the same list with two other RNGs running as well. open $fh, '>', 'rng-3'; $ground = Math::Random::MT->new(5); $noise1 = Math::Random::MT->new($$); $noise2 = Math::Random::MT->new(time); for (1 .. $iters) { $noise1->rand(); print {$fh} $ground->irand, "\n"; $noise2->rand(); $noise2->irand(); } close $fh; undef $ground; undef $noise1; undef $noise2; print "Done!\n"; exit; Then I diff'd rng-1 against rng-2 and rng-3. Identical. And speedy, too... generated the 3 sets of 10,000 pretty much instantly. Now I just have to get it installed site-wide at work, a process that can take weeks, if not months... (and no smart remarks about local::lib, cpanm, etc... this is a large corporate environment with its own rules and ways of doing things, unfortunately.) Randy -- """ Randy J. Ray Sunnyvale, CA http://www.rjray.org rj...@blackperl.com twitter.com/rjray Silicon Valley Scale Modelers: http://www.svsm.org
Re: Random Perl Content
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 1:214532096,2062273046 2:222611822 1:586787367,1729346018 2:1419417079 perl -Mstrict -MMath::Rand48=nrand48 -wle ' ... ' 1 2 1 2 1:214532096 2:222611822,1419417079 1:2062273046 2:327232521,342130822 -- Ruud
Re: Random Perl Content
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. -- Ruud
Re: Random Perl Content
When looking for answers to these questions, I always get out my 'Microprocessor programming for computer hobbyists (1977)' which is invaluable. 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 etc. Iain. On 18 May 2013 23:21, Dave Hodgkinson wrote: > Individual random number daemons? > > Sent from my iPhone > > On 18 May 2013, at 21:53, "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. > > > > Randy > > -- > > > """ > > Randy J. Ray Sunnyvale, CA http://www.rjray.org > rj...@blackperl.com > > twitter.com/rjray > > Silicon Valley Scale Modelers: http://www.svsm.org > >
Re: Random Perl Content
Individual random number daemons? Sent from my iPhone On 18 May 2013, at 21:53, "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. > > Randy > -- > """ > Randy J. Ray Sunnyvale, CA http://www.rjray.org rj...@blackperl.com > twitter.com/rjray > Silicon Valley Scale Modelers: http://www.svsm.org
Re: Random Perl Content
I do not know of any of the modules that does has what you need. I do understand your problem as I once needed something likewise for 'random' worksheets with Math excercises. What I ended up was doing what I did on my old TI-59 to generate random numbers. take a seed <0 .. 1.0> multiply it by Pi chop of the integer part and save that as the new seed that can't be too difficult ? Op May 18, 2013, om 10:53 PM heeft Randy J. Ray het volgende geschreven: > (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 rj...@blackperl.com > twitter.com/rjray > Silicon Valley Scale Modelers: http://www.svsm.org
Re: Random Perl Content
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. Abigail