[vox-tech] random number in C
Peter Jay Salzman
vox-tech@lists.lugod.org
Tue, 2 Apr 2002 14:47:20 -0800
begin Bill Broadley <bill@math.ucdavis.edu>
> On Tue, Apr 02, 2002 at 10:05:34AM -0800, Peter Jay Salzman wrote:
> > i wrote some monte carlo code which obtains a random generator seed
> > through a read to /dev/random.
> >
> > on a lark, i did a run:
> >
> > T: 4.0e+00 beta: 2.500e-01 trials: 1000000
> > N: 10 M: 10 seed: 208006379
>
> What code did you use to read from /dev/random(provide source)?
> What exact call did you call with rand() (provide source)?
> How exactly did you initialize the random number generator (provide source)?
>
> You did pass the seed to srand() and not rand() right?
here's the code:
int main(int argc, char *argv[])
{
unsigned int seed;
...
seed = SeedRandomGenerator();
...
printf("\nT: %.1Le beta: %.3Le trials: %lu N: %u M: %u seed: %u\n",
T, beta, trials, N, M, seed);
}
and here is my seed function:
unsigned int SeedRandomGenerator(void)
{
unsigned int seed;
FILE *fp;
if( (fp = fopen("/dev/random", "r")) == NULL)
die("Can't open /dev/random for reading.");
fread(&seed, sizeof(unsigned int), 1, fp);
fclose(fp);
srand(seed);
return seed;
}
ok, so i run the program. here's what i get:
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
so now i comment out the call to SeedRandomGenerator() and replace it by
int main(int argc, char *argv[])
{
...
/* seed = SeedRandomGenerator(); */
seed = 3497451914;
...
printf("\nT: %.1Le beta: %.3Le trials: %lu N: %u M: %u seed: %u\n",
T, beta, trials, N, M, seed);
}
and here's what i get:
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
the seed is supposedly the same, but i get different results. from
here, when i re-run the program, i keep getting:
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
which is what i would expect to happen.
i take this to mean that seed doesn't have the same value between the
run with SeedRandomGenerator() and the run with SeedRandomGenerator()
commented out.
but darned if i know. they look the same. :(
> > the seed for these two runs is the same. i would've expected that calls
> > to rand() should've generated the same set of random numbers. yet the
> > different runs of the monte carlo code yield different results, which is
> > fine, except i was expecting it to be exactly the same since the seed is
> > the same.
>
> A reasonable expectation.
>
> If you port to any other platform you should use random and srandom
> which provides for higher quality implemtnations on most platforms
> (besides linux). It also allows you to set a more complex seed.
i'll keep this in mind. thanks for the tip.
i can't port this code to other platforms as-is since i have code that
catches SIGFPE, which is linux specific. i'm thinking of writing a
solaris port so i can run my jobs on isun.
just kidding. :)
pete