Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Legacy code uses "srand()" .. how to avoid losing entropy?

by sundialsvc4 (Abbot)
on May 03, 2011 at 16:00 UTC ( [id://902743]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Legacy code uses "srand()" .. how to avoid losing entropy?
in thread Legacy code uses "srand()" .. how to avoid losing entropy?

Since I can’t rely upon “that version of Perl,” my present train(-wreck) of thought is to either bring in a separate random-number object for this purpose, or to do the “re-seed with a random number picked just before re-seeding” strategy described earlier.   (It is good to know that Perl is getting this “return the previous seed value” capability, as many other languages do have it, but it’s too late for this (cr)app.)

Thanks for your inputs, one and all.   Now, back to the coal-mines.

Replies are listed 'Best First'.
Re^4: Legacy code uses "srand()" .. how to avoid losing entropy?
by furry_marmot (Pilgrim) on May 03, 2011 at 17:45 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://902743]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-24 06:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found