Thread View: sci.math.stat
4 messages
4 total messages
Started by Jim Buddenhagen
Sat, 25 Jul 1992 19:16
random number generator
Author: Jim Buddenhagen
Date: Sat, 25 Jul 1992 19:16
Date: Sat, 25 Jul 1992 19:16
25 lines
1742 bytes
1742 bytes
(apologies if you see this twice -- I posted from another machine but
it didn't seem to make it to the network)
Portable pseudo-random number generator desired, for uniform and/or normal
deviates. What is desired is code (C preferred) or algorithm that is
machine independent (i.e. same seed, same sequence regardless of computing
platform). Purpose will be to generate random datasets (or pointers into
databases) for benchmark tests accross several platforms. Must be able to
generate several independent sequences 'simultaneously' (ok to have several
copies of routine with different names).
I am aware of routines in Numerical Recipies, and in Knuth, but unsure
of quality. Would welcome any comments, suggestions, pointers to code,
or references.
----
Jim Buddenhagen Southwestern Bell Telephone Co.
jb1556@swuts.sbc.com Statistical Research
(previous line email preferred)
314-235-5183 One Bell Center, Room 27-X-5
St. Louis, MO 63130
--
The opinions expressed are not necessarily those of the University of
North Carolina at Chapel Hill, the Campus Office for Information
Technology, or the Experimental Bulletin Board Service.
internet: bbs.oit.unc.edu or 152.2.22.80
C source for Park-Miller "minimal std. generator":
Author: 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
Re: C source for Park-Miller "minimal std. generator":
Author: Nick Maclaren
Date: Mon, 27 Jul 1992 09:11
Date: Mon, 27 Jul 1992 09:11
34 lines
2048 bytes
2048 bytes
In article <l73ggjINNh52@almaak.usc.edu>, ajayshah@almaak.usc.edu (Ajay Shah) writes: |> #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. */ |> |> ... PLEASE, PLEASE don't use this generator for serious work (i.e. any project which uses more than a million numbers or so IN ALL) without checking! Park and Miller's paper is quite good, but is rather optimistic in their ideas of what is a minimal reliable generator. Unfortunately, the same is true of Marsaglia's (with co-authors) papers "Towards a Universal Random Number Generator" and "A Random Number Generator for PCs", and Marsaglia is one of the people who lead this field! Good RNGs are very, very hard to find. Any generator which EITHER has a 32-bit seed OR is based on conventional single-precision starts to give seriously misleading results for sequences of a few million numbers. See N.M. Maclaren "A Limit of the Usable Length of a Pseudo-random Sequence" J.Statist.Comput.Simul. 42 pp 47-54 for the proof of the first statement - the proof of the second is trivial to derive using the same techniques, because of the 24-bit precision limit. Nick Maclaren University of Cambridge Computer Laboratory, New Museums Site, Pembroke Street, Cambridge CB2 3QG, England. Email: nmm@cl.cam.ac.uk Tel.: +44 223 334761 Fax: +44 223 334679
Re: random number generator
Author: Nick Maclaren
Date: Mon, 27 Jul 1992 09:21
Date: Mon, 27 Jul 1992 09:21
38 lines
2439 bytes
2439 bytes
In article <1992Jul25.191638.16109@samba.oit.unc.edu>, Jim.Buddenhagen@bbs.oit.unc.edu (Jim Buddenhagen) writes: |> Portable pseudo-random number generator desired, for uniform and/or normal |> deviates. What is desired is code (C preferred) or algorithm that is |> machine independent (i.e. same seed, same sequence regardless of computing |> platform). Purpose will be to generate random datasets (or pointers into |> databases) for benchmark tests accross several platforms. Must be able to |> generate several independent sequences 'simultaneously' (ok to have several |> copies of routine with different names). |> |> I am aware of routines in Numerical Recipies, and in Knuth, but unsure |> of quality. Would welcome any comments, suggestions, pointers to code, |> or references. There are NO generators currently available that can produce multiple sequences with any reasonable degree of independence, and almost every expert (including Knuth) warns against misusing single generators in this way. Almost the best that you can do is to get non-overlapping sequences, which is NOT the same thing! Most of the papers that purport to describe generators for independent sequences use definitions of independence that are completely unheard of by statisticians. The main exceptions that I know of are various papers by A. De Matteis and others (which are well worth reading) and N.M. Maclaren "The Generation of Multiple Independent Sequences of Pseudorandom Numbers" Appl.Statist. (1989) 38 pp 351-359. But PLEASE note the provisos in the latter about the criterion used for independence - there is more justification than there was room for in the paper, but little is certain in this area. Nick Maclaren University of Cambridge Computer Laboratory, New Museums Site, Pembroke Street, Cambridge CB2 3QG, England. Email: nmm@cl.cam.ac.uk Tel.: +44 223 334761 Fax: +44 223 334679
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads