It sounds like you're getting wrapped up in a few problems:

  1. You've got code that depends on a stable list of pseudo-random numbers.
  2. The pseudo-random numbers aren't random enough and the results are skewed.
  3. There are calls to srand($n) throughout the program.
  4. You want to capture the "state" of the RNG at various points.

So tackle these problems separately. As a kludge, you should be able to make the program run as-is, but with better results.

  1. Follow JavaFan's (and others') suggestion and pre-generate a list of better random numbers. There are more elegant ways to do this, but for the sake of the concept, store a list of a million pre-generated random numbers in the __DATA__ section of the code, pull them up from <DATA> and put them in a global array @RAND, accessed with a global $COUNTER. External sources might complicate matters at this stage, which is why I suggest throwing it in the code.
  2. Then borrow Tye's suggestion to override srand and rand for your purposes:
    BEGIN { our @RAND = (); our $COUNTER = 0; for (<DATA>) { $_ += 0; push @RAND, $_; } *CORE::GLOBAL::srand = sub(;$){ $COUNTER = 0 }; *CORE::GLOBAL::rand = sub(){ return $RAND[$COUNTER++] }; }
  3. Now srand($n) will ignore $n, but will reset $COUNTER so the sequence can start again. And rand will act as expected, but will return the number from @RAND rather than generate it on the fly. From the perspective of the rest of the code, this should be transparent, obviating the need for global changes (as in search/replace) to the code.
  4. To capture the state of the "RNG" (now a list), simply capture $COUNTER to pick up where you left off later.

--marmot


In reply to Re^4: Legacy code uses "srand()" .. how to avoid losing entropy? by furry_marmot
in thread Legacy code uses "srand()" .. how to avoid losing entropy? by locked_user sundialsvc4

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.