Problem this random seed()

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Mar 19 11:09:08 EDT 2013


On Tue, 19 Mar 2013 07:44:47 -0700, NZach wrote:

> OK, i changed the code again. Delete the G class (The purpose of G class
> was to refer to global variables). Add Rnd.seed(12345) in main()
> function. The new code : http://codeviewer.org/view/code:30da
> 
> i print the Rnd.expovariate(ArrivalClass.ArvRate).
> 
> The output i get be executing the above code is the following : ---
> 0.134729071364
> 0.00255530717358
> 0.0886834413113
> 
> Result =  0.0571622124959
> 0.134729071364
> 0.00255530717358
> 0.0886834413113
> 
> Result =  0.0453791550084
> ---
> 
> 
> So, the problem is probably with time (which is what @Stev mentioned
> before).

Who is "Stev"? If you mean me, Steve or Steven, I did not say anything 
about time. I mentioned the random number generator *seed*.

The random number generator cannot read your mind and automatically reset 
back to the start just because you want it to reset. You have to actually 
set the seed to the same value it was.


py> from random import Random
py> Rnd = Random(12345)
py> Rnd.expovariate(2)
0.43779052477592995
py> Rnd.expovariate(2)
2.2941973691140807
py> Rnd.seed(12345)
py> Rnd.expovariate(2)
0.43779052477592995
py> Rnd.expovariate(2)
2.2941973691140807


> But i still cant understand the reason. From the SimPy documentation :
> http://simpy.sourceforge.net/SimPyDocs/Manuals/SManual.html it says for
> the initialize(): "The initialize statement initialises global
> simulation variables and sets the software clock to 0.0. It must appear
> in your program before any SimPy process objects are activated."

That has nothing to do with the random numbers generated by expovariate().



-- 
Steven



More information about the Python-list mailing list