Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Storing the state of the random number generator

by toma (Vicar)
on Oct 24, 2001 at 10:03 UTC ( #121005=perlquestion: print w/ replies, xml ) Need Help??
toma has asked for the wisdom of the Perl Monks concerning the following question:

I would like to be to store the state of the random number generator. I would like to specify the seed value on the first run of the program, but then after that I want the random number generator to pick up where it left off. I want to store the last state of the random number generator in a file. The next time I run the program, I want it to read the file and put the random number generator back into its previous state.

Is this possible in Perl? I did this in once in C a long time ago, and now I want to do it again in perl. The linux (or BSD 4.3) initstate() and setstate() calls enable this capability in C.

It should work perfectly the first time! - toma

Comment on Storing the state of the random number generator
Re: Storing the state of the random number generator
by jeroenes (Priest) on Oct 24, 2001 at 10:13 UTC
    It seems that there doesn't exist a state saver in perl. I searched CPAN for random, but nothing with a state saving function. If there does exist such a thing in gnu libs, I would suggest you just make an XS interface to it.

    Jeroen
    "We are not alone"(FZ)

Re: Storing the state of the random number generator
by jlongino (Parson) on Oct 24, 2001 at 10:48 UTC
    I believe you can simulate the behaviour by storing the original seed, and the number of times you called rand. A crude example (just so you get the idea):
    use strict; my $savedseed = 1234567; srand $savedseed; my $savedct = 0; my $rnd; foreach (1..5) { $savedct = $_; $rnd = rand; print "$savedct: $rnd\n"; } print "\n"; srand $savedseed; foreach (1..$savedct) { $rnd = rand; print "$_: $rnd\n"; } print "\n"; print $savedct+1, ": ", rand, "\n";
    which produces:
    1: 0.03509521484375 2: 0.139495849609375 3: 0.08056640625 4: 0.35931396484375 5: 0.123077392578125 1: 0.03509521484375 2: 0.139495849609375 3: 0.08056640625 4: 0.35931396484375 5: 0.123077392578125 6: 0.814605712890625
    HTH

    --Jim

    Update: Capitalizing on the fact that "srand $seed" will produce the same sequence of numbers as long as the same value for $seed is used. See srand and seed for more details.

Re: Storing the state of the random number generator
by Fletch (Chancellor) on Oct 24, 2001 at 18:15 UTC

    initstate() and setstate() have to do with the random() call, but perl may not be using random() so they'd be of no use to you. You can tell with: perl -Vrandfunc. For example, 5.6.1 compiled on RedHat 7.1 with the defaults is using drand48(), not random()

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://121005]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (14)
As of 2014-07-28 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (201 votes), past polls