It sounds like you're getting wrapped up in a few problems:
- You've got code that depends on a stable list of pseudo-random numbers.
- The pseudo-random numbers aren't random enough and the results are skewed.
- There are calls to srand($n) throughout the program.
- 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.
- 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.
- 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++] };
}
- 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.
- To capture the state of the "RNG" (now a list), simply capture $COUNTER to pick up where you left off later.
--marmot