Home > Archive > PERL Miscellaneous > October 2004 > What's the seed?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Derek Fountain 2004-10-24, 3:55 am |
| 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. Sinan Unur 2004-10-24, 3:55 am |
| Derek Fountain <nospam@example.com> wrote in
news:417b1c6f$0$13760$5a62ac22@per-qv1-newsreader-01.iinet.net.au:
> 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
| |
| Abigail 2004-10-24, 8:55 pm |
| Derek Fountain (nospam@example.com) wrote on MMMMLXXII September MCMXCIII
in <URL:news:417b1c6f$0$13760$5a62ac22@per-qv1-newsreader-01.iinet.net.au>:
[] 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?
Not from standard Perl. Perhaps if you write a piece of XS.
[] 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 do believe "time ^ ($$ + ($$ << 15))" is a good seed to defend against
exactly what you are describing. Many years ago, the manual page suggested
to use "time ^ $$", until I noticed a problem if you run the same program
repeatedly in a small time frame. Analysis shows that
time ^ $$ == (time + 1) ^ ($$ + 1)
one third of a time. They are identical if time and $$ end in the same
number of 1s.
That's why I suggested to use "time ^ ($$ + ($$ << 15))", which doesn't
suffer from that problem, and that because the standard suggestion until
Perl set a seed by itself (one based on various things, including the
output of ps, the current time and the process id).
Here's a link to the relevant post:
http://www.google.nl/groups?q=srand...w.iaf.nl&rnum=3
Abigail
--
perl -we 'print q{print q{print q{print q{print q{print q{print q{print q{print
qq{Just Another Perl Hacker\n}}}}}}}}}' |\
perl -w | perl -w | perl -w | perl -w | perl -w | perl -w | perl -w | perl -w
| |
| ctcgag@hotmail.com 2004-10-25, 4:00 pm |
| Derek Fountain <nospam@example.com> wrote:
> 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
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
| |
| Ben Morrow 2004-10-26, 3:57 pm |
|
Quoth ctcgag@hotmail.com:
> Derek Fountain <nospam@example.com> wrote:
>
> 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
| |
| ctcgag@hotmail.com 2004-10-27, 3:56 am |
| Ben Morrow <usenet@morrow.me.uk> wrote:
> Quoth ctcgag@hotmail.com:
>
> 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
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
| |
| Tassilo v. Parseval 2004-10-27, 3:56 am |
| Also sprach ctcgag@hotmail.com:
> Ben Morrow <usenet@morrow.me.uk> wrote:
>
> 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[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{re
htonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
| |
| Ben Morrow 2004-10-27, 8:56 am |
|
Quoth ctcgag@hotmail.com:
> Ben Morrow <usenet@morrow.me.uk> wrote:
>
> 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 {/.(.)/s} 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
| |
| ctcgag@hotmail.com 2004-10-27, 3:57 pm |
| Ben Morrow <usenet@morrow.me.uk> wrote:
> Quoth ctcgag@hotmail.com:
>
> 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
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
| |
| ctcgag@hotmail.com 2004-10-27, 3:57 pm |
| "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> wrote:
> Also sprach ctcgag@hotmail.com:
>
>
> 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
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
|
|
|
|
|