Problem this random seed()

N

NZach

Hello everyone,

i am using the MMK.py example from a SimPy tutorial, which can be found here: http://heather.cs.ucdavis.edu/~matloff/156/PLN/DESimIntro.pdf

I have made very little changes to the code and i have upload it here:
http://codeviewer.org/view/code:30d3

The problem is that i am executing the main() function in lines 83 and 90, but
i do not receive the same result, although the random seed is used in the G
class.

Any idea please, how can i solve it ?


Thanks,

Nicholas.
 
E

eli m

Hello everyone,



i am using the MMK.py example from a SimPy tutorial, which can be found here: http://heather.cs.ucdavis.edu/~matloff/156/PLN/DESimIntro.pdf



I have made very little changes to the code and i have upload it here:

http://codeviewer.org/view/code:30d3



The problem is that i am executing the main() function in lines 83 and 90, but

i do not receive the same result, although the random seed is used in the G

class.



Any idea please, how can i solve it ?





Thanks,



Nicholas.

What errors did it show?
 
A

alex23

The problem is that i am executing  the main() function in lines 83 and90, but
i do not receive the  same result, although the random seed is used in the G
class.

Any idea please, how can i solve it ?

The seed is used to create a Random instance at the very start of the
module. This means it will have the same state for each run of the
module, but not for each execution of the main function. To have each
execution of main() return the same result, you will need to set the
seed _within_ the main function.

Try adding this as the first line in main:

G.Rnd = Random(12345)

This should ensure that the state of your random generator is the same
for each execution of the main() function and any subsequent functions
it calls that rely on G.Rnd.
 
N

NZach

@eli m : The problem is that i execute on lines 83 and 90 the main() and although the seed value is the same, equal to 12345, i receive different results from lines 85 and 92.

@alex23: I changed the code, http://codeviewer.org/view/code:30d4, but i still receive have the same problem. The results are different.

Any help please
 
N

NZach

@eli m : The problem is that i execute on lines 83 and 90 the main() and although the seed value is the same, equal to 12345, i receive different results from lines 85 and 92.

@alex23: I changed the code, http://codeviewer.org/view/code:30d4, but i still receive have the same problem. The results are different.

Any idea please
 
S

Sven

@eli m : The problem is that i execute on lines 83 and 90 the main() and
although the seed value is the same, equal to 12345, i receive different
results from lines 85 and 92.

@alex23: I changed the code, http://codeviewer.org/view/code:30d4, but i
still receive have the same problem. The results are different.

Any idea please

have you actually tried printing out the return value of

G.Rnd.expovariate(MachineClass.SrvRate)

?

I'm not sure as I have not used SimPy before, but the values you are
printing out at the bottom seem to be related to the timing, specifically
now(), which would give you a different result each time.

If you print out or inspect the result of expovariate, it should be the
same each time you run main.
 
N

NZach

Thank you @sven ! I thought that the time was reset automatically after calling the second main. Yes, i checked the values and they are the same.
 
N

NZach

Thank you @sven ! I thought that the time was reset automatically after calling the second main. Yes, i checked the values and they are the same.
 
N

NZach

Sorry, my previous reply was completely wrong. The time is initialized through the initialize() to 0. So, each time i call main() the simulation time is initialized to 0.

I changed the code in order to print and check the values of

G.Rnd.expovariate(MachineClass.SrvRate) and G.Rnd.expovariate(ArrivalClass.ArvRate)

here is the code and the changes i made : http://codeviewer.org/view/code:30d8

The values are not the same.

The result i take is the following:

Result from main() call No. 1 is 0.0452927906737
Result from main() call No. 2 is 0.0588336669949
2
First time Execution of Main Main
[0.07267291894782457, 0.019146562687989536, 0.034780398625615376, 0.005528912370370478, 0.023636815811338075]
[0.13472907136407936, 0.0025553071735785462, 0.08868344131130483, 0.05381286556098766, 0.044091180681591464]
Second time Execution of Main
[0.0335704752213292, 0.1321512230812724, 0.16025979406301488, 0.029210377271523574, 0.006680846943670858]
[0.20642889529587374, 0.047894131266223446, 0.10958802111803392, 0.02393344456847461, 0.13280785022932287]
 
N

NZach

Sorry, my previous reply was completely wrong. The time is initialized through the initialize() to 0. So, each time i call main() the simulation time is initialized to 0.

I changed the code in order to print and check the values of

G.Rnd.expovariate(MachineClass.SrvRate) and G.Rnd.expovariate(ArrivalClass.ArvRate)

here is the code and the changes i made : http://codeviewer.org/view/code:30d8

The values are not the same.

The result i take is the following:

Result from main() call No. 1 is 0.0452927906737
Result from main() call No. 2 is 0.0588336669949
2
First time Execution of Main Main
[0.07267291894782457, 0.019146562687989536, 0.034780398625615376, 0.005528912370370478, 0.023636815811338075]
[0.13472907136407936, 0.0025553071735785462, 0.08868344131130483, 0.05381286556098766, 0.044091180681591464]
Second time Execution of Main
[0.0335704752213292, 0.1321512230812724, 0.16025979406301488, 0.029210377271523574, 0.006680846943670858]
[0.20642889529587374, 0.047894131266223446, 0.10958802111803392, 0.02393344456847461, 0.13280785022932287]
 
S

Steven D'Aprano

Sorry, my previous reply was completely wrong. The time is initialized
through the initialize() to 0. So, each time i call main() the
simulation time is initialized to 0.

I changed the code in order to print and check the values of

G.Rnd.expovariate(MachineClass.SrvRate) and
G.Rnd.expovariate(ArrivalClass.ArvRate)

here is the code and the changes i made :
http://codeviewer.org/view/code:30d8

The values are not the same.

Why you think that they should be? They are using random values, of
course they will be different, until you reset the seed.

py> from random import expovariate, seed
py> seed(123)
py> [expovariate(1) for i in range(3)]
[2.949543607473089, 2.4397037404431217, 0.8983482559638]
py> [expovariate(1) for i in range(3)]
[2.2284035134067692, 0.10402931548508174, 3.2661334288222377]
py> seed(123) # reset the seed
py> [expovariate(1) for i in range(3)]
[2.949543607473089, 2.4397037404431217, 0.8983482559638]


In your case, you initialise the seed once, using:


class G:
Rnd = Random(12345)



so when you want to run the same results again, you have to call:

G.Rnd.seed(12345)


By the way, what is the purpose of class G?

Also, you import expovariate from the Random module, but never use it.
 
N

NZach

Probably a better code : http://codeviewer.org/view/code:30d9

I run the program once using comment in line 39 and then using comment in line 62.

The results are not the same. The problem is with the Random. Although, i use seed value equal to 12345, i do not take the same results. :[

Any idea please
 
S

Sven

Sorry, my previous reply was completely wrong. The time is initialized
through the initialize() to 0. So, each time i call main() the simulation
time is initialized to 0.

I changed the code in order to print and check the values of

G.Rnd.expovariate(MachineClass.SrvRate) and
G.Rnd.expovariate(ArrivalClass.ArvRate)

here is the code and the changes i made :
http://codeviewer.org/view/code:30d8

The values are not the same.

The result i take is the following:

Result from main() call No. 1 is 0.0452927906737
Result from main() call No. 2 is 0.0588336669949
2
First time Execution of Main Main
[0.07267291894782457, 0.019146562687989536, 0.034780398625615376,
0.005528912370370478, 0.023636815811338075]
[0.13472907136407936, 0.0025553071735785462, 0.08868344131130483,
0.05381286556098766, 0.044091180681591464]
Second time Execution of Main
[0.0335704752213292, 0.1321512230812724, 0.16025979406301488,
0.029210377271523574, 0.006680846943670858]
[0.20642889529587374, 0.047894131266223446, 0.10958802111803392,
0.02393344456847461, 0.13280785022932287]

In the new code you are creating one instance of G which you seed with 1234
and then calling expovariate on it each time you call main without
reseeding. You're only seeding it once and reusing it, so you would get
different values. What you had before, where you seeded G.Rnd in the main
was fine.

I also suggest you change one thing at a time, rather than reworking large
parts each iteration. All I was suggesting was a simple print statement in
the Run method like you had it before
 
N

NZach

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).

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."

Any idea why that happens ?
 
N

NZach

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).

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."

Any idea why that happens ?
 
S

Sven

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).

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."

Any idea why that happens

I am guessing it's because now gives you the current clock value. Yes,
initialise sets it to 0.0, but on execution now will change to the time
since initialise(), so you will get varying values. That's my best guess
 
S

Steven D'Aprano

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().
 
S

Sven

On 19 March 2013 15:09, Steven D'Aprano <
Who is "Stev"? If you mean me, Steve or Steven, I did not say anything
about time. I mentioned the random number generator *seed*.
I think he meant me.

That has nothing to do with the random numbers generated by expovariate().
No, but in early versions he was printing out the time (or a time related
term) and was wondering why that is different too.
 
N

NZach

Τη ΤÏίτη, 19 ΜαÏτίου 2013 3:57:30 Ï€.μ. UTC+2, ο χÏήστης NZach έγÏαψε:
Hello everyone,



i am using the MMK.py example from a SimPy tutorial, which can be found here: http://heather.cs.ucdavis.edu/~matloff/156/PLN/DESimIntro.pdf



I have made very little changes to the code and i have upload it here:

http://codeviewer.org/view/code:30d3



The problem is that i am executing the main() function in lines 83 and 90, but

i do not receive the same result, although the random seed is used in the G

class.



Any idea please, how can i solve it ?





Thanks,



Nicholas.

Yes, sorry, I meant @sven not @Stev.

The problem with the random number is solved.
 
N

NZach

Τη ΤÏίτη, 19 ΜαÏτίου 2013 5:23:47 μ.μ. UTC+2, ο χÏήστης NZach έγÏαψε:
Τη ΤÏίτη, 19 ΜαÏτίου 2013 3:57:30 Ï€.μ. UTC+2, ο χÏήστης NZach έγÏαψε:




Yes, sorry, I meant @sven not @Stev.



The problem with the random number is solved.

Thank you very much for the replies.

I will check what the now() prints and probably if i wont find any solutioni will create new topic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top