Re: [Tutor] printing the random seed?

2006-02-02 Thread Kent Johnson
Danny Yoo wrote:
 
 On Thu, 2 Feb 2006, kevin parks wrote:
 
 
Danny (hope you are good!)  co,

I see that biz about random.seed()... but in the absence of setting that
... does it just grab a value from the system clock?
 
 
 Yes.  Here's what the documentation says officially:
 
 current system time is also used to initialize the generator when the
 module is first imported

As of Python 2.4, random.seed() will attempt to use os.urandom() to 
initialize the seed; if that is not available it uses the system time.

Is there a way to just let it generate it's usual, known seed... but
then observe what that is in case you get an especially good run of
data?
 
 
 We can call seed() explicitely using system time then when we start using
 the random module, and if the results are interesting, we report that
 initial seed value too.  That way, by knowing the initial conditions, we
 can reproduce the results.

Here is the code from random.py that initializes the seed (a):
 try:
 a = long(_hexlify(_urandom(16)), 16)
 except NotImplementedError:
 import time
 a = long(time.time() * 256) # use fractional seconds

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] printing the random seed?

2006-02-01 Thread Danny Yoo


 I am having some fun with python and making multiple runs on an
 algorhythm and sometimes getting some fun stuff that i would like to be
 able to reproduce, but there are some random elements in it. I wonder is
 there a way to see the random seed, and make note of it so that you
 could then set the seed for a subsequent run to get the same (initially)
 random results?

Hi Kevin,

Sure; take a look at the seed() function in the random module.

http://www.python.org/doc/lib/module-random.html#l2h-1298

Just set it to some consistant value at the very beginning of your
program, and you should then see duplicate results between program runs.

You could also probably do something with random.getstate() and
random.setstate(), but it's probably a bit simpler just to inject a known
seed value into the pseudorandom generator.


   (Hi Danny, if you are still here!)

*wave wave*


Good luck!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] printing the random seed?

2006-02-01 Thread kevin parks
Danny (hope you are good!)  co,

I see that biz about random.seed()... but in the absence of setting 
that ... does it
just grab a value from the system clock?

Is there a way to just let it generate it's usual, known seed... but 
then observe
what that is in case you get an especially good run of data?

like i clearly can't just go:

zeed = random.seed()
print zeed = , zeed

hmm...

cheers,

-[kp]--

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] printing the random seed?

2006-02-01 Thread Danny Yoo


On Thu, 2 Feb 2006, kevin parks wrote:

 Danny (hope you are good!)  co,

 I see that biz about random.seed()... but in the absence of setting that
 ... does it just grab a value from the system clock?

Yes.  Here's what the documentation says officially:

current system time is also used to initialize the generator when the
module is first imported


 Is there a way to just let it generate it's usual, known seed... but
 then observe what that is in case you get an especially good run of
 data?

We can call seed() explicitely using system time then when we start using
the random module, and if the results are interesting, we report that
initial seed value too.  That way, by knowing the initial conditions, we
can reproduce the results.

##
 import time
 import random

 t = time.time()

 random.seed(t)
 random.random()
0.39026231885512619
 random.random()
0.72296902513427053
 random.random()
0.48408173490166762

 t
1138866167.719857

 random.seed(t)
 random.random()
0.39026231885512619
 random.random()
0.72296902513427053
 random.random()
0.48408173490166762
##

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor