Random long-number generator
Mike Caprio
caprio at uhccux.uhcc.hawaii.edu
Mon Feb 5 11:06:04 AEST 1990
>Nope, no such luck. You see, what I am usually looking for is a good
>(best would be _fine_!) RNG which uses integer arithmetic only. Why?
>Because I'm a cheapskate who refuses to invest in a floating point
>processor for my PC, and also if I ever get around to writing a program
>good enough to be worth distributing or (choke) selling, I want it to be
>able to run well on as many systems as possible, so FP is out - too many
>emulators are TOO SLOW!
>
>So, how about it?
>
The following random number generator might be of
interest to you. It produces integers in the range 0-2^31, with
a period of approx. 2^31. It was published in Modeling and
Simulation on Micro- computers (see ref. below), and was tested
extensively by the authors. The original code was fortran, but
I've run this on Turbo C and the output matched the test output
listed in the article. I would check it against that listing if
you use a different compiler to be sure. If you can't get ahold
of the journal, E-MAIL me and I'll send along at least part of
the listing.
For the curious, the bit about seed<0 and then adding
2x31 is apparently a quick way to do modulo 2^31 with signed
longs. Incidently, this generator is quite fast, quicker than
the integer random generator built into Turbo C.
long seed;
long lrand ()
{
seed = seed * 1220703125L;
if (seed < 0)
seed += 2147483648L;
}
From:
Dudewicz, E.J., Z. A. Karian and R. J. Marshall III. 1985.
Random number generation on microcomputers. pp. 9-14. In R. G.
Lavery [ed.], Modeling and simulation on microcomputers:1985.
Society for Computer Simulation, La Jolla.
_Mike_ caprio at uhccux
Opinons? I had one last week...
More information about the Comp.lang.c
mailing list