For use in a shuffling algorithm, I need to be able to generate repeatable pseudo random sequences of integers in a range 1-N, such that given the same (integer) seed, the same sequence will always be produced.

I currently do:

srand($seed) ... $j = int rand ($n + 1); ...
inside a Fisher-Yates shuffle algorithm. This works, but also "taints" the random number generator by setting the seed to a deterministic value. Since this code runs under mod_perl, it is possible that later code will require a random number that is not so deterministically chosen, so I would like to avoid leaving the seed set to a deterministic value.

Is there a way to find out what the current seed is, so that I can restore it after generating the pseudo random sequence? Would calling srand() with no arguments after the deterministic sequence generation is complete be adequate, with regard to what perldoc -f srand says?

     Do not call "srand" multiple times in your program
     unless you know exactly what you're doing and why
     you're doing it.  The point of the function is to
     "seed" the "rand" function so that "rand" can pro­
     duce a different sequence each time you run your
     program.  Just do it once at the top of your pro­
     gram, or you won't get random numbers out of
     "rand"!

In reply to Generating Repeatable Pseudorandom Sequences by mp

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.