Article View: sci.math.stat
Article #82C source for Park-Miller "minimal std. generator":
From: Ajay Shah
Date: Sat, 25 Jul 1992 21:05
Date: Sat, 25 Jul 1992 21:05
62 lines
2330 bytes
2330 bytes
#include <stdio.h>
/* This file implements the "minimal standard" RNG
from the paper "RNGs: Good Ones are Hard to Find" by Park and
Miller, CACM, Volume 31, Number 10, October 1988. */
static long int seed = 1;
/* static so programs will be more "orderly" in modifying the
seed. The setseed function is the prescribed way in which the seed
is altered. A good default to use is your SSN.
Any seed from 1 to 2147483646 is equally ok. */
int setseed(long int newseed)
{
if ((newseed < 1) || (newseed > 2147483646)) return 1;
seed = newseed; return 0;
}
double ranpm()
/* page 1195-right, "Integer Version 2", works iff ints are 32 bits.*/
{
long int a=16807, m=2147483647, q=127773, r=2836;
int lo, hi, test;
hi = seed / q;
lo = seed % q;
test = a * lo - r * hi;
seed = (test > 0) ? test : test + m;
return (seed/ (double) m);
}
#ifdef TESTING
/* They give us one test (on page 1195-left): if seed = 1 then
the 10001'th random number should be 1043618065.
Apart from this, I do some superficial testing aimed at checking
the code above is not buggy. I'm not trying to test the
statistical properties of the RNG. */
int main()
{
int n; double u, sum=0.0, sum2=0.0;
for (n=1; n<=10000; n++) {
u = ranpm();
if ((u < 0.0) || (u > 1.0)) {
printf("Garbage at the %dth number!\n");
return 1;
}
sum += u;
sum2 += (u*u);
}
printf("The current value of seed is : %10d\n", seed);
printf("The Truth is %17s 1043618065\n\n", " ");
printf("Mean of 1st 10k nums is %f, variance is %f\n",
sum/10000, sum2/9999);
printf("Expect .5018 and .3355\n");
return 0;
}
#endif /* TESTING */
--
Ajay Shah, (213)749-8133, ajayshah@usc.edu
Technical Details
Message-ID:
<l73ggjINNh52@almaak.usc.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!usc!newsReferences:
<1992Jul25.191638.16109@samba.oit.unc.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!usc!news From: ajayshah@almaak.usc.edu (Ajay Shah) Newsgroups: sci.math.stat Subject: C source for Park-Miller "minimal std. generator": Date: 25 Jul 1992 14:05:23 -0700 Organization: University of Southern California, Los Angeles, CA Lines: 62 Sender: ajayshah@almaak.usc.edu (Ajay Shah) Message-ID: <l73ggjINNh52@almaak.usc.edu> References: <1992Jul25.191638.16109@samba.oit.unc.edu> NNTP-Posting-Host: almaak.usc.edu Keywords: pseudo-random, C , uniform, normal