# What's the seed?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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!

## Re: What's the seed?

> 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

## Re: What's the seed?

> 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

--
Usenet Newsgroup Service                        \$9.95/Month 30GB

## Re: What's the seed?

Quoth ctcgag@hotmail.com:
> > 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

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

--
Razors pain you / Rivers are damp
Acids stain you / And drugs cause cramp.                    [Dorothy Parker]
Guns aren't lawful / Nooses give
Gas smells awful / You might as well live.                   ben@morrow.me.uk

## Re: What's the seed?

> Quoth ctcgag@hotmail.com:
> > > 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
>
> 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

--
Usenet Newsgroup Service                        \$9.95/Month 30GB

## Re: What's the seed?

Also sprach ctcgag@hotmail.com:

>> Quoth ctcgag@hotmail.com:
>> > > 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
>>
>> 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.

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
--
\$_=q#",}])!JAPH!qq(tsuJ[3..0}_\$;//::niam/s~=)]3[))_\$-3(rellac(=_\$({
pam)(rekcah)(lreP)!JAPH!qq(rehtona{tsuJbus#;
\$_=reverse,s+(?<=sub).+q#q!'"qq.\t\$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval

## Re: What's the seed?

> Also sprach ctcgag@hotmail.com:
>
> >> Quoth ctcgag@hotmail.com:
> >> > > 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
> >>
> >> 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.
>
> 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

--
Usenet Newsgroup Service                        \$9.95/Month 30GB

## Re: What's the seed?

Quoth ctcgag@hotmail.com:
> > Quoth ctcgag@hotmail.com:
> > > >
> > > > single, steps. I have my suspicions about the quality of my seed!
> > >
> > > 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.
>
> 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

--
perl -e'print map sort unpack "a2"x26, pack "N"x13,
qw/1632265075 1651865445 1685354798 1696626283 1752131169 1769237618
1801808488 1830841936 1886550130 1914728293 1936225377 1969451372
2047502190/'                                                 # ben@morrow.me.uk

## Re: What's the seed?

> Quoth ctcgag@hotmail.com:
> > > Quoth ctcgag@hotmail.com:
> > > > >
> > > > > single, steps. I have my suspicions about the quality of my seed!
> > > >
> > > > 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.
> >
> > 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.

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

--