Code Comments
Programming Forum and web based access to our favorite programming groups.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!
Post Follow-up to this messageDerek 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
Post Follow-up to this messageDerek 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 ru ns, [] 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{pr int qq{Just Another Perl Hacker\n}}}}}}}}}' |\ perl -w | perl -w | perl -w | perl -w | perl -w | perl -w | perl -w | perl - w
Post Follow-up to this messageDerek 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
Post Follow-up to this messageQuoth 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.u k
Post Follow-up to this messageBen 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
Post Follow-up to this messageAlso 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
Post Follow-up to this message
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
Post Follow-up to this messageBen 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
Post Follow-up to this message"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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.