What's the seed?

D

Derek Fountain

In any recent version of Perl, the seed for the random number generator is
set at the first time rand() is called. Can I find out what that seed is so
I can subsequently reproduce the random sequence?

At present I'm setting my own seed using "time ^ ($$ + ($$ << 15))", but my
program is being run repeatedly over and over, and only takes a fraction of
a second to do its job. That means time is often the same for several runs,
and $$ tends to go up in small, sometimes single, steps. I have my
suspicions about the quality of my seed!
 
A

A. Sinan Unur

In any recent version of Perl, the seed for the random number
generator is set at the first time rand() is called. Can I find out
what that seed is so I can subsequently reproduce the random sequence?

It might have been nice to srand return the current seed for this purpose,
but given that the standard C srand function also does not return the seed,
I have a feeling there is a good reason for it.

What I have always done is to find a good way of generating seeds and then
save them.
At present I'm setting my own seed using "time ^ ($$ + ($$ << 15))",
but my program is being run repeatedly over and over, and only takes a
fraction of a second to do its job. That means time is often the same
for several runs, and $$ tends to go up in small, sometimes single,
steps. I have my suspicions about the quality of my seed!

I am going to suggest using the Math::Random package.

#! perl

use strict;
use warnings;

use Math::Random;

my $s = [ random_get_seed() ];

my @series;

for my $i ( 0 .. 1 ) {
$series[$i] = [ random_uniform(10, 0, 1) ];
random_set_seed(@$s);
}

use Data::Dumper;
print Dumper \@series;


__END__

Sinan
 
C

ctcgag

Derek Fountain said:
In any recent version of Perl, the seed for the random number generator
is set at the first time rand() is called. Can I find out what that seed
is so I can subsequently reproduce the random sequence?

At present I'm setting my own seed using "time ^ ($$ + ($$ << 15))", but
my program is being run repeatedly over and over, and only takes a
fraction of a second to do its job. That means time is often the same for
several runs, and $$ tends to go up in small, sometimes single, steps. I
have my suspicions about the quality of my seed!

I think this would be generally adequate, but you don't say what you are
using this for. If you are concerned, and you think that perl's default
srand is better than what you are doing, I would do something like this:

my $x=int rand(~0); #Cause srand to be invoked "naturally"
srand($x);
warn "using $x as seed"; #record seed

Xho
 
B

Ben Morrow

Quoth (e-mail address removed):
I think this would be generally adequate, but you don't say what you are
using this for. If you are concerned, and you think that perl's default
srand is better than what you are doing, I would do something like this:

my $x=int rand(~0); #Cause srand to be invoked "naturally"
srand($x);
warn "using $x as seed"; #record seed

I think this is incorrect: the seed contains *more* entropy than any
given return value from rand(), so this will give you less random
numbers than not setting the seed at all.

Ben
 
C

ctcgag

Ben Morrow said:
Quoth (e-mail address removed):

I think this is incorrect: the seed contains *more* entropy than any
given return value from rand(), so this will give you less random
numbers than not setting the seed at all.

But the primary purpose wasn't to create a seed with more entropy
than perl's default seed, but rather to know what the seed was so it could
be re-used in the future.

Xho
 
T

Tassilo v. Parseval

Also sprach (e-mail address removed):
But the primary purpose wasn't to create a seed with more entropy
than perl's default seed, but rather to know what the seed was so it could
be re-used in the future.

There's no way of getting this information. The seed is calculated using
Perl_seed() (in util.c). Its return value is used to initialize the
libc's random number generator but it's not stored anywhere. That means
one has to call srand() manually if one intends to produce the same
sequence of random number all over again.

Tassilo
 
B

Ben Morrow

Quoth (e-mail address removed):
But the primary purpose wasn't to create a seed with more entropy
than perl's default seed, but rather to know what the seed was so it could
be re-used in the future.

Yes, but (I think) that your method will give a seed with substantially
*less* entropy than the perl default, which given the OP's comments
about the quality of his seed I would take to be a Bad Thing.

Ben
 
C

ctcgag

Ben Morrow said:
Quoth (e-mail address removed):

Yes, but (I think) that your method will give a seed with substantially
*less* entropy than the perl default, which given the OP's comments
about the quality of his seed I would take to be a Bad Thing.

My interpretation was that the OP was worried about the time-variance
of the initial seed quality, not the total theoretical entropy of the seed
over infinite time. (That is why I asked what he was using it for.) For
almost all applications, I'd gladly give up a little overall total entropy
to get better time-variance entropy. (And for those application where I
wouldn't, I wouldn't use the default RNG at all, regardless of seed)

With all the stuff that "make" and does to header files, and with the
defining and redefining of preprocessor macros, I'm having a hard time
figuring out what the perl C code looks like at the instant before the
Perl source compiles. But as far as I can tell, the seed is a U32
with 32 bits, and the loss of entropy associated with re-seeding would be
less than one bit.

Xho
 
C

ctcgag

Tassilo v. Parseval said:
Also sprach (e-mail address removed):


There's no way of getting this information. The seed is calculated using
Perl_seed() (in util.c). Its return value is used to initialize the
libc's random number generator but it's not stored anywhere. That means
one has to call srand() manually if one intends to produce the same
sequence of random number all over again.

I know. Calling srand() manually is what I proposed doing.

I just proposed a different way of arriving at the seed
which is passed to srand().

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top