Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Getting srand's seed

by fangly (Initiate)
on Oct 09, 2008 at 23:46 UTC ( #716343=perlquestion: print w/replies, xml ) Need Help??
fangly has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
It's got to be easy... however I spent hours searching how to do it unsucessfully.
I just want some random numbers such as the ones produced by rand(). I don't even want to bother creating my own seeds for the random number generator, but I need to retrieve the seed to be able to regenerate the exact same series of random numbers in case I need it. According to the documentation srand() is automatically called by Perl.
Is it possible to retrieve what seed value srand() has used for the random number generator?? I think it's possible in Java, Matlab, ... In a desperate attempt, I tried $seed = srand(); but that returns 1 all the time.

Replies are listed 'Best First'.
Re: Getting srand's seed
by GrandFather (Sage) on Oct 10, 2008 at 00:05 UTC

    There are two possibilities: it is (as you suggest) easy, or it is impractical. Given that the srand documentation doesn't describe a way to do it I'd guess it is impractical.

    A workaround that may be good enough for you is:

    my $srandSeed = rand (2**31); srand ($srandSeed);

    which uses the standard technique for seeding, then reseeds using the first "random" number generated. Note however that Perl's built in rand has many problems that may be an issue depending in your application. For serious work you would be much better using Math::Random::MT or one of the other random number generating modules.

    Perl reduces RSI - it saves typing
      The thing is that trying with my own value is really not an optimal way. Perl should really be able to return the value of the seed it uses.

      I tried using my own value with:
      $seed = int(rand(1e10));
      But then some numbers appeared way more often than you would expect.
      Using that worked much better:
      $seed = int(rand(2**31));
      No idea why...

      But since I'm messing around with creating my own seed, I will probably go with the "recommended" way (in the srand function documentation):
      $seed = time ^ ($$ + ($$ << 15));

      I looked at the Math::Random::* option. It seems like Math::Random has a random_get_seed function, which is really what I want. But do I want to add this module dependency, just to retrieve a seed value??

        There are a number of really important things that you haven't told us. The most important pertains to the specific build of Perl that you are using because that influences the number of bits that rand uses for its seed. For standard Windows builds of Perl 2**15 values are all there are!

        I suggested $seed = int(rand(2**31)); because most current Perl builds handle 32 bit integers without trouble and that would encompass the majority of seed sizes you are likely to encounter with standard builds of Perl. It also has the large advantage over using something like $seed = time ^ ($$ + ($$ << 15)); that the full entropy used to generate the "system" seed is retained.

        We can't tell what you really want unless you tell us the nature of your application. If all you are doing is generating mazes for net hack then there really isn't an issue.

        Perl reduces RSI - it saves typing
        Perl should really be able to return the value of the seed it uses.

        No. It's a feature that it doesn't return its seed somehow. The random number generator is there to generate random data, to the extend that this is practical on a deterministic machine.

        So it's a feature that the sequence which rand() returns isn't easily reproducible - unless you've taken care to make it so, by first calling srand.

Re: Getting srand's seed
by roboticus (Chancellor) on Oct 10, 2008 at 00:01 UTC

    Rather than try to retrieve the random seed, just call srand() again with your value to give a repeatable sequence, like so:

    $ perl -e 'srand(7); print int(rand(10)),"\n" for 1..10' 2 6 2 1 4 2 1 7 0 0 $ perl -e 'srand(7); print int(rand(10)),"\n" for 1..10' 2 6 2 1 4 2 1 7 0 0
Re: Getting srand's seed
by kubrat (Scribe) on Oct 10, 2008 at 10:06 UTC

    In this discussion an interesting question has been raised. How does perl generate its seed? Does anyone know?

      When in doubt just RTFS (search for Perl_seed) . . .

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://716343]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2018-05-25 22:56 GMT
Find Nodes?
    Voting Booth?