[vox-tech] Re: random number in C

Peter Jay Salzman vox-tech@lists.lugod.org
Wed, 3 Apr 2002 08:22:35 -0800


i'm guilty of screwing up my cut and paste -- they were from different
machines.   the mystery was real.  i just showed the wrong output.

the problem was that i set the seed variable and didn't call srand.

i was making all sorts of mistakes yesterday.  that's what you get when
you work 10 hours straight without taking a break.  :(

pete



begin Matt Holland <mdholland@ucdavis.edu> 
> Jeff Newmiller wrote:
> 
> >The random driver may be used to obtain a seed for random() or rand(), or
> >it could be used to obtain random numbers on its own.  You run a risk of
> >extracting all of the randomness from the system if you depend on random
> >for long random sequences, though, and a /dev/random that has run out of
> >entropy is a file that will block until that condition changes.
> >
> >Having known psuedo-random sequences does allow you to "repeat"
> >monte-carlo simulations.  To answer Peter, yes, rand() is supposed to be
> >deterministic for this very reason.  I am puzzled by the same issue Ken is
> >puzzled by, though... failing to call srand() should always cause rand()
> >to yield the same pseudo-random sequence... it should never have yielded
> >variable results in the first place!
> 
> 
> I don't think there's any mystery here.  If you compare the two sets of 
> runs (late in the day vs. early in the day), you will see that the early 
> runs have "trials: 1000000", whereas the later runs have "trials: 1000". 
>  I'm assuming that "trials" is some parameter that probably influences 
> the number of random numbers generated within a simulation, so you 
> wouldn't expect the same answer even with the same seed, since a 
> parameter has been changed.
> 
> Matt
> 
> >
> >
> >>On Tuesday 02 April 2002 06:26 pm, you wrote:
> >>
> >>>>Date: Tue, 2 Apr 2002 14:47:20 -0800
> >>>>[CODE SNIPPED]
> >>>>
> >>>So this is when the program is seeded to /dev/random:
> >>>
> >>>>  p@satan% ./ising2-jfunc 4.0
> >>>>  T: 4.0e+00  beta: 2.500e-01  trials: 1000  N: 10  M: 10  seed:
> >>>>3497451914 Average Energy: -1.273893
> >>>>  Average magnetization: 0.425220
> >>>>  Average |magnetization|: 0.531100
> >>>>  Average magnetic susceptibility: 34.746600
> >>>>
> >>>And this is when the code is seeded to 1:
> >>>
> >>>>  T: 4.0e+00  beta: 2.500e-01  trials: 1000  N: 10  M: 10  seed:
> >>>>3497451914 Average Energy: -1.326747
> >>>>  Average magnetization: 0.495320
> >>>>  Average |magnetization|: 0.495320
> >>>>  Average magnetic susceptibility: 28.645040
> >>>>
> >>>And you've established that if you run it repeatedly you keep getting 
> >>>the 
> >>same thing:
> >>
> >>>>  T: 4.0e+00  beta: 2.500e-01  trials: 1000  N: 10  M: 10  seed:
> >>>>3497451914 Average Energy: -1.326747
> >>>>  Average magnetization: 0.495320
> >>>>  Average |magnetization|: 0.495320
> >>>>  Average magnetic susceptibility: 28.645040
> >>>>
> >>>BUT four hours earlier:
> >>>
> >>>>Date: Tue, 2 Apr 2002 10:05:34 -0800
> >>>>
> >>>This is when you seed it to /dev/random:
> >>>
> >>>>	T: 4.0e+00  beta: 2.500e-01  trials: 1000000
> >>>>	N: 10  M: 10  seed: 208006379
> >>>>	Average Energy: -1.057143
> >>>>	Average magnetization: 0.002008
> >>>>	Average |magnetization|: 0.251765
> >>>>	Average magnetic susceptibility: 8.948655
> >>>>
> >>>And this is when you seed it to 1:
> >>>
> >>>>	T: 4.0e+00  beta: 2.500e-01  trials: 1000000
> >>>>	N: 10  M: 10  seed: 208006379
> >>>>	Average Energy: -1.050182
> >>>>	Average magnetization: 0.030723
> >>>>	Average |magnetization|: 0.246092
> >>>>	Average magnetic susceptibility: 8.692724
> >>>>
> >>>Why did 4 hours of time change the results when you seed it to 1 ???
> >>>_______________________________________________
> >>>vox-tech mailing list
> >>>vox-tech@lists.lugod.org
> >>>http://lists.lugod.org/mailman/listinfo/vox-tech
> >>>
> >>_______________________________________________
> >>vox-tech mailing list
> >>vox-tech@lists.lugod.org
> >>http://lists.lugod.org/mailman/listinfo/vox-tech
> >>
> >>
> >
> >---------------------------------------------------------------------------
> >Jeff Newmiller                        The     .....       .....  Go Live...
> >DCN:<jdnewmil@dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
> >                                      Live:   OO#.. Dead: OO#..  Playing
> >Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> >/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...2k
> >---------------------------------------------------------------------------
> >
> >
> >_______________________________________________
> >vox-tech mailing list
> >vox-tech@lists.lugod.org
> >http://lists.lugod.org/mailman/listinfo/vox-tech
> >
> 
> 
> 
> _______________________________________________
> vox-tech mailing list
> vox-tech@lists.lugod.org
> http://lists.lugod.org/mailman/listinfo/vox-tech