home *** CD-ROM | disk | FTP | other *** search
- double random()
-
- /*
- This Turbo C V1.5 function returns a double pseudo-random number in
- the range: 0.0 < random() < 1.0
-
- The period of this function is 2,147,483,646 (2**31-2); that is, it
- will generate 2,147,483,636 pseudo-random numbers before repeating
- the series.
-
- Examples:
- x = 2.0*random()-1.0;
- if random < 0.5 lower;
-
- In the first example, X is assigned a random number in the range
- of: -1.0 < X < +1.0. In the second example, procedure lower is
- called fifty percent of the time. (i.e. When the value returned by
- random is less than one-half.)
-
- Note: The random number seed, "seed", MUST be declared as a global
- variable of type "long int". (i.e. An integer variable
- capable of representing numbers as large as 2,147,483,647)
- and MUST be initialized in the main procedure to a number
- in the range: 0 < seed < 2147483647 before this function
- is invoked.
-
- This function is based on a Pascal routine given in the following
- reference:
-
- Park, Stephen K., & Miller, Keith W., "Random Number Generators:
- Good Ones are Hard to Find", in "Communications of the ACM", Vol. 31,
- No. 10 (October 1988), pp 1192 - 1201. Abstract: "Practical and
- theoretical issues are presented concerning the design, implementa-
- tion, and use of a good, minimal standard random number generator
- that will port to virtually all systems."
-
- Function coded by Harry M. Murphy on 31 October 1988. */
-
- {
- /* A = the multiplier.
- M = the modulus = 2^31-1
- Q = M / A
- R = M % A */
-
- #define A 16807
- #define M 2147483647
- #define Q 127773
- #define R 2836
-
- long int lo, hi, temp;
-
- hi = seed / Q;
- lo = seed % Q;
- temp = A*lo - R*hi;
- if (temp <= 0)
- seed = temp + M;
- else
- seed = temp;
- return (double) seed / M; }