I'm performing a lengthy computation that uses a lot of calls to rand, and I want to be able to pause the computation and resume it again later (using Storable or some other serialization). The process starts with a random seed that I provide, so that I can reproduce interesting results later. I want the results to be the same for a given initial random seed regardless of whether I paused/resumed the computation, so I need to somehow save a random seed in my serialization.

The only reasonable* solution I could think of was something along these lines:

## either load the saved random seed and state data from the ## serialization (resume a paused compuatation), or start with ## new state data and the given random seed (start new ## compuation) my ($foo, $bar, $random_seed) = deserialize('saved') || initial_values(); while (1) { srand($random_seed); ## some crunching on $foo, $bar, using lots of rand() ## decide what the next iteration's seed will be $random_seed = int rand( 2 << 31 ); if (check_for_user_interrupt()) { ## save state, along with the seed for next iteration serialize('saved', $foo, $bar, $random_seed); exit; } }
I use rand to generate a new seed for the next iteration, so it can be saved after any iteration. As soon as I wrote srand inside my while loop, red flags popped up in my head. The srand perldoc says:
Do not call srand() multiple times in your program unless you know exactly what you're doing and why you're doing it.
... but I wonder if I really know exactly what I'm doing ;). I found at least one post regarding multiple srand calls, but in that case the poster was using something like time ^ $$ to re-seed within a very fast loop, and expecting different random output for each loop.

What I would really like to know is in what situations is ok to call srand multiple times? Is using a call to rand for a seed considered bad? I can't use any external entropy to re-seed, because I want all the results to be based on the original random seed supplied at the beginning of the computation. Any thoughts on the serialization of randomly-generated state data like this?

*: In the worst case I could always wrap rand, save the original random seed, and simply save the number of times rand was called. Then to resume again, I just reseed, and call rand in void context a whole slug of times. I'd rather not do this!

blokhead


In reply to Can I seed srand() with results from rand()? by blokhead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.