Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41470 > unrolled thread
| Started by | NZach <nickzacharatos@gmail.com> |
|---|---|
| First post | 2013-03-18 18:57 -0700 |
| Last post | 2013-03-19 08:26 -0700 |
| Articles | 20 — 5 participants |
Back to article view | Back to comp.lang.python
Problem this random seed() NZach <nickzacharatos@gmail.com> - 2013-03-18 18:57 -0700
Re: Problem this random seed() eli m <techgeek201@gmail.com> - 2013-03-18 19:36 -0700
Re: Problem this random seed() alex23 <wuwei23@gmail.com> - 2013-03-18 19:51 -0700
Re: Problem this random seed() NZach <nickzacharatos@gmail.com> - 2013-03-19 03:52 -0700
Re: Problem this random seed() NZach <nickzacharatos@gmail.com> - 2013-03-19 03:54 -0700
Re: Problem this random seed() Sven <svenito@gmail.com> - 2013-03-19 11:59 +0000
Re: Problem this random seed() NZach <nickzacharatos@gmail.com> - 2013-03-19 05:27 -0700
Re: Problem this random seed() NZach <nickzacharatos@gmail.com> - 2013-03-19 05:27 -0700
Re: Problem this random seed() NZach <nickzacharatos@gmail.com> - 2013-03-19 06:50 -0700
Re: Problem this random seed() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-19 14:18 +0000
Re: Problem this random seed() Sven <svenito@gmail.com> - 2013-03-19 14:19 +0000
Re: Problem this random seed() NZach <nickzacharatos@gmail.com> - 2013-03-19 07:44 -0700
Re: Problem this random seed() Sven <svenito@gmail.com> - 2013-03-19 15:05 +0000
Re: Problem this random seed() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-19 15:09 +0000
Re: Problem this random seed() Sven <svenito@gmail.com> - 2013-03-19 15:19 +0000
Re: Problem this random seed() NZach <nickzacharatos@gmail.com> - 2013-03-19 07:44 -0700
Re: Problem this random seed() NZach <nickzacharatos@gmail.com> - 2013-03-19 06:50 -0700
Re: Problem this random seed() NZach <nickzacharatos@gmail.com> - 2013-03-19 07:18 -0700
Re: Problem this random seed() NZach <nickzacharatos@gmail.com> - 2013-03-19 08:23 -0700
Re: Problem this random seed() NZach <nickzacharatos@gmail.com> - 2013-03-19 08:26 -0700
| From | NZach <nickzacharatos@gmail.com> |
|---|---|
| Date | 2013-03-18 18:57 -0700 |
| Subject | Problem this random seed() |
| Message-ID | <c447cffe-964f-4f8e-9931-0c3b5ff9bf6d@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | eli m <techgeek201@gmail.com> |
|---|---|
| Date | 2013-03-18 19:36 -0700 |
| Message-ID | <4b13ad53-a269-437d-a4df-6ba52c56c167@googlegroups.com> |
| In reply to | #41470 |
On Monday, March 18, 2013 6:57:30 PM UTC-7, NZach wrote: > 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?
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-03-18 19:51 -0700 |
| Message-ID | <b8e31f24-aa86-46cd-bb8f-21a7e50bebf5@o9g2000pbt.googlegroups.com> |
| In reply to | #41470 |
On Mar 19, 11:57 am, NZach <nickzachara...@gmail.com> wrote:
> 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 ?
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.
[toc] | [prev] | [next] | [standalone]
| From | NZach <nickzacharatos@gmail.com> |
|---|---|
| Date | 2013-03-19 03:52 -0700 |
| Message-ID | <0d49facc-b14d-4818-9cf3-d750181d79f8@googlegroups.com> |
| In reply to | #41474 |
@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
[toc] | [prev] | [next] | [standalone]
| From | NZach <nickzacharatos@gmail.com> |
|---|---|
| Date | 2013-03-19 03:54 -0700 |
| Message-ID | <be57c21d-7f3c-473b-b553-8a68b84331db@googlegroups.com> |
| In reply to | #41470 |
@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
[toc] | [prev] | [next] | [standalone]
| From | Sven <svenito@gmail.com> |
|---|---|
| Date | 2013-03-19 11:59 +0000 |
| Message-ID | <mailman.3495.1363694370.2939.python-list@python.org> |
| In reply to | #41486 |
[Multipart message — attachments visible in raw view] — view raw
On 19 March 2013 10:54, NZach <nickzacharatos@gmail.com> wrote: > @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. -- ./Sven
[toc] | [prev] | [next] | [standalone]
| From | NZach <nickzacharatos@gmail.com> |
|---|---|
| Date | 2013-03-19 05:27 -0700 |
| Message-ID | <618539ea-7a9e-4cd1-8413-22a8491077e8@googlegroups.com> |
| In reply to | #41487 |
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.
[toc] | [prev] | [next] | [standalone]
| From | NZach <nickzacharatos@gmail.com> |
|---|---|
| Date | 2013-03-19 05:27 -0700 |
| Message-ID | <mailman.3496.1363696085.2939.python-list@python.org> |
| In reply to | #41487 |
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.
[toc] | [prev] | [next] | [standalone]
| From | NZach <nickzacharatos@gmail.com> |
|---|---|
| Date | 2013-03-19 06:50 -0700 |
| Message-ID | <a40ab4ab-a9cb-4a96-a4b6-d4eaa09dd8a4@googlegroups.com> |
| In reply to | #41487 |
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]
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-03-19 14:18 +0000 |
| Message-ID | <514873bc$0$6599$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #41493 |
On Tue, 19 Mar 2013 06:50:34 -0700, NZach wrote:
> 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.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Sven <svenito@gmail.com> |
|---|---|
| Date | 2013-03-19 14:19 +0000 |
| Message-ID | <mailman.3506.1363702753.2939.python-list@python.org> |
| In reply to | #41493 |
[Multipart message — attachments visible in raw view] — view raw
On 19 March 2013 13:50, NZach <nickzacharatos@gmail.com> wrote: > 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 -- ./Sven
[toc] | [prev] | [next] | [standalone]
| From | NZach <nickzacharatos@gmail.com> |
|---|---|
| Date | 2013-03-19 07:44 -0700 |
| Message-ID | <00329c02-21b7-438e-8551-0f4677d01474@googlegroups.com> |
| In reply to | #41504 |
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 ?
[toc] | [prev] | [next] | [standalone]
| From | Sven <svenito@gmail.com> |
|---|---|
| Date | 2013-03-19 15:05 +0000 |
| Message-ID | <mailman.3516.1363705546.2939.python-list@python.org> |
| In reply to | #41511 |
[Multipart message — attachments visible in raw view] — view raw
On 19 March 2013 14:44, NZach <nickzacharatos@gmail.com> 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). > > 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 -- ./Sven
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-03-19 15:09 +0000 |
| Message-ID | <51487f93$0$30001$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #41511 |
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
[toc] | [prev] | [next] | [standalone]
| From | Sven <svenito@gmail.com> |
|---|---|
| Date | 2013-03-19 15:19 +0000 |
| Message-ID | <mailman.3518.1363706384.2939.python-list@python.org> |
| In reply to | #41516 |
[Multipart message — attachments visible in raw view] — view raw
On 19 March 2013 15:09, Steven D'Aprano < steve+comp.lang.python@pearwood.info> wrote: > 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*. > > I think he meant me. > > 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(). > > No, but in early versions he was printing out the time (or a time related term) and was wondering why that is different too. -- ./Sven
[toc] | [prev] | [next] | [standalone]
| From | NZach <nickzacharatos@gmail.com> |
|---|---|
| Date | 2013-03-19 07:44 -0700 |
| Message-ID | <mailman.3526.1363709362.2939.python-list@python.org> |
| In reply to | #41504 |
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 ?
[toc] | [prev] | [next] | [standalone]
| From | NZach <nickzacharatos@gmail.com> |
|---|---|
| Date | 2013-03-19 06:50 -0700 |
| Message-ID | <mailman.3504.1363701744.2939.python-list@python.org> |
| In reply to | #41487 |
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]
[toc] | [prev] | [next] | [standalone]
| From | NZach <nickzacharatos@gmail.com> |
|---|---|
| Date | 2013-03-19 07:18 -0700 |
| Message-ID | <2d0604f8-7700-4dee-8a62-69c94d908ee6@googlegroups.com> |
| In reply to | #41470 |
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
[toc] | [prev] | [next] | [standalone]
| From | NZach <nickzacharatos@gmail.com> |
|---|---|
| Date | 2013-03-19 08:23 -0700 |
| Message-ID | <729b7df7-d1b5-4397-b659-fdab25f10dd2@googlegroups.com> |
| In reply to | #41470 |
Τη Τρίτη, 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.
[toc] | [prev] | [next] | [standalone]
| From | NZach <nickzacharatos@gmail.com> |
|---|---|
| Date | 2013-03-19 08:26 -0700 |
| Message-ID | <9bb3bf94-5e26-4c97-af10-7650f110e0f1@googlegroups.com> |
| In reply to | #41519 |
Τη Τρίτη, 19 Μαρτίου 2013 5:23:47 μ.μ. UTC+2, ο χρήστης 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. Thank you very much for the replies. I will check what the now() prints and probably if i wont find any solution i will create new topic.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web