Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » sci.math.* » sci.stat.math.independent

Topic: Random numbers for C: Improvements.
Replies: 38   Last Post: Jan 21, 1999 4:19 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
George Marsaglia

Posts: 363
Registered: 12/7/04
Random numbers for C: Improvements.
Posted: Jan 15, 1999 11:41 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

As I hoped, several suggestions have led to
improvements in the code for RNG's I proposed for
use in C. (See the thread "Random numbers for C: Some
suggestions" in previous postings.) The improved code
is listed below.

A question of copyright has also been raised. Unlike
DIEHARD, there is no copyright on the code below. You
are free to use it in any way you want, but you may
wish to acknowledge the source, as a courtesy.

To avoid being cited by the style police, some have
suggested using typedef rather than #define in order
to replace unsigned long by UL.

Others have pointed out that one cannot be certain of
the way that a compiler will evaluate terms in a sum,
so using ++c in a term is dangerous. They have
offered a version using the comma operator, which
ensures that the table index is incremented properly.
See LFIB4 and SWB below.

In my earlier work, done in Fortran, I had implemented
two 16-bit multiply-with-carry generators, say z and w,
as 32-bit integers, with the carry in the top 16 bits,
the output in the bottom 16. They were combined by
(z<<16)+w. (In Fortran, ishft(z,16)+w.) Such a
combination seemed to pass all tests. In the above-
mentioned post, I used (z<<16)+(w&65525), and that
does not pass all tests. So (z<<16)+w seems
preferable; it is used below, providing a MWC that
seems to pass all tests.

The generators MWC, KISS, LFIB4 and SWB seem to pass all tests.
By themselves, CONG and SHR3 do not, but using
CONG+SHR3 provides one of the fastest combinations that satisfy
the DIEHARD battery of tests.

Of course, one cannot have absolute confidence in any
generator. The choices LFIB4 and SWB have immense
periods, are very fast, and pass all tests in DIEHARD,
but I am hesitant to rely on them alone---primarily
because they come from such simple mod 2^32 arithmetic:
four adds in LFIB4 or one subtract-with-borrow in SWB.

The code below provides a variety of in-line generators that
seem promising by themselves, and even more so in combination.
With them, one may feel fairly confident that combinations
will produce results consistent with the underlying probability theory
in your applications.


All combinations seem to support the supplemented quote
from my 1984 Keynote Address:

A random number generator is like sex;
When it's good, it's wonderful,
And when it's bad, it's still pretty good.

And when it's bad, try a twosome or threesome.

The C code follows; you may want to snip and save from
here--------------------------------------------------:

#define znew (z=36969*(z&65535)+(z>>16))
#define wnew (w=18000*(w&65535)+(w>>16))
#define MWC ( (znew<<16)+wnew )
#define SHR3 (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5))
#define CONG (jcong=69069*jcong+1234567)
#define KISS ((MWC^CONG)+SHR3)
#define LFIB4 (t[c]=t[c]+t[c+58]+t[c+119]+t[c+178],t[++c])
#define SWB (t[c+237]=(x=t[c+15])-(y=t[c]+(x<y)),t[++c])
#define UNI (KISS*2.328306e-10)
#define VNI ((long) KISS)*4.656613e-10
typedef unsigned long UL;

/* Global static variables: */
static UL z=362436069, w=521288629, jsr=123456789, jcong=380116160;
static UL t[256];
static UL x=0,y=0; static unsigned char c=0;
/* Random seeds must be used to reset z,w,jsr,jcong and
the table t[256]. Here is an example procedure, using KISS: */

void settable(UL i1,UL i2,UL i3,UL i4)
{ int i; z=i1;w=i2,jsr=i3; jcong=i4;
for(i=0;i<256;i++) t[i]=KISS; }

/* Any one of KISS, MWC, LFIB4, SWB, SHR3, or CONG can be used in
an expression to provide a random 32-bit integer, while UNI
provides a real in (0,1) and VNI a real in (-1,1). */






Date Subject Author
1/15/99
Read Random numbers for C: Improvements.
George Marsaglia
1/15/99
Read Re: Random numbers for C: Improvements.
Dmitri Zaykin
1/15/99
Read Re: Random numbers for C: Improvements.
Radford Neal
1/15/99
Read Re: Random numbers for C: Improvements.
Jeff Stout
1/15/99
Read Re: Random numbers for C: Improvements.
Radford Neal
1/16/99
Read Re: Random numbers for C: Improvements.
Dmitri Zaykin
1/16/99
Read Re: Random numbers for C: Improvements.
Dmitri Zaykin
1/17/99
Read Re: Random numbers for C: Improvements.
Ramin Sina
1/18/99
Read Re: Random numbers for C: Improvements.
Edward C. Hook
1/19/99
Read Re: Random numbers for C: Improvements.
Orjan Johansen
1/19/99
Read Re: Random numbers for C: Improvements.
Horst Kraemer
1/17/99
Read Re: Random numbers for C: Improvements.
Herman Rubin
1/17/99
Read Re: Random numbers for C: Improvements.
Duncan Murdoch
1/18/99
Read Re: Random numbers for C: Improvements.
Herman Rubin
1/18/99
Read Re: Random numbers for C: Improvements.
Radford Neal
1/19/99
Read Re: Random numbers for C: Improvements.
Herman Rubin
1/18/99
Read Re: Random numbers for C: Improvements.
Duncan Murdoch
1/19/99
Read Re: Random numbers for C: Improvements.
Herman Rubin
1/19/99
Read Re: Random numbers for C: Improvements.
Duncan Murdoch
1/17/99
Read Re: Random numbers for C: Improvements.
David W. Noon
1/17/99
Read Re: Random numbers for C: Improvements.
Dmitri Zaykin
1/18/99
Read Re: Random numbers for C: Improvements.
David W. Noon
1/18/99
Read Re: Random numbers for C: Improvements.
Dmitri Zaykin
1/18/99
Read Re: Random numbers for C: Improvements.
Duncan Murdoch
1/20/99
Read Re: Random numbers for C: Improvements.
Dmitri Zaykin
1/20/99
Read Re: Random numbers for C: Improvements.
Dmitri Zaykin
1/21/99
Read Re: Random numbers for C: Improvements.
John E. Davis
1/21/99
Read Re: Random numbers for C: Improvements.
Dmitri Zaykin
1/18/99
Read Re: Random numbers for C: Improvements.
Mike McCarty
1/18/99
Read Re: Random numbers for C: Improvements.
Herman Rubin
1/19/99
Read Re: Random numbers for C: Improvements.
David W. Noon
1/19/99
Read Re: Random numbers for C: Improvements.
Herman Rubin
1/19/99
Read Re: Random numbers for C: Improvements.
David W. Noon
1/20/99
Read Re: Random numbers for C: Improvements.
Dmitri Zaykin
1/18/99
Read Re: Random numbers for C: Improvements.
Mike McCarty
1/18/99
Read Re: Random numbers for C: Improvements.
Herman Rubin
1/21/99
Read Re: Random numbers for C: Improvements.
Mike McCarty
1/16/99
Read Re: Random numbers for C: Improvements.
Dmitri Zaykin
1/19/99
Read Re: Random numbers for C (and assembly)
qscgz@my-dejanews.com

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.