http://qs321.pair.com?node_id=195172


in reply to Generating Repeatable Pseudorandom Sequences

I guess I am missing the point of the question, first you ask:
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.
Then:
so I would like to avoid leaving the seed set to a deterministic value.

So it looks like you are asking for 2 exclusive things? rephrased it looks like "I need to be able to reproduce the random number sequence generated" and then "I don’t like being able to reproduce the random number sequence generated". Can you clear up what you are asking here? I guess the bottom line the way the question stands is that you _need_ a known seed to reproduce the random sequence...

Thanks!
-Waswas
  • Comment on Re: Generating Repeatable Pseudorandom Sequences

Replies are listed 'Best First'.
Re: Re: Generating Repeatable Pseudorandom Sequences
by mp (Deacon) on Sep 04, 2002 at 19:56 UTC
    Sorry if I was unclear. The requirements are not mutually exclusive, I just need different types of "random" numbers at different places in the code. The shuffling subroutine needs to use a deterministic sequence of pseudo random (not truly random) numbers that are repeatable given the same seed. Other code outside the shuffle algorithm may eventually need to use random numbers based on rand() that are not repeatable like those used in the shuffle algorithm. Since this is a rather large persistent (mod_perl) application, it is difficult to foresee where rand() will be used in other code outside the shuffle subroutine, so I would like to avoid "tainting" it with a deterministic seed, or at least clean up afterwards by using a good seed.

    What I am writing is a shuffle that can be seeded to always produce exactly one permutation per seed, but that when run with different seeds (seeds that are sequential integers, with some gaps) will produce a set of permutations that are reasonably uniformly distributed. I.e., if I sort the array qw(a b c d) 2400 times using 2400 different seeds, I should expect to see each of the 24 possible permutations about 100 times (plus/minus some tolerance).

    Thanks to input from dws above, here is the code I wound up using, which meets my requirements and avoids altering the seed used by rand().

    use Math::Random::MT (); sub shuffle { my $self = shift; my $seed = shift; my $array = $self; # ||rand() in case no seed passed to shuffle algorithm my $mt = Math::Random::MT->new( $seed || rand ); my $i; for($i = @$array; --$i; ) { my $j = int( $mt->rand($i + 1) ); @$array[$i, $j] = @$array[$j, $i]; } }
    Thank you all for the replies.