#include "random.h" float random(int& seed){ /* Author: Glen Cowan Date: 3-OCT-1997 C++ version: 23-Aug-1999 Simon George Multiplicative linear congruential random number generator. The value RANDOM is uniformly distributed in [0,1]. The first value of the input variable seed can be any large (32 bit) integer. The next call to RANDOM should use the last ouput value of seed*. Modulus and multiplier values according to P.L. L'Ecuyer, Comm. ACM 31 (1988) 742. See also S. Brandt, "Datenanalyse", BI-Wissenschaftsverlag, Mannheim (1992), p. 76. NOTE: This routine is for mainly for pedagogical purposes and simple applications. For more sophisticated algorithms see e.g. the CERN program library (routines RANMAR, RANLUX) and reviews such as F. James, "A review of pseudorandom number generators", Comp. Phys. Comm. 60 (1990) 329. * C++ note: the argument is declared as int& so that it is passed by reference for this purpose. */ const int a = 40692; // the multiplier const int m = 2147483399; // the modulus const int q = m/a; int k, r; // The algorithm below implements seed = MOD(a*seed,m) without producing // integer values outside [-m,m] (which would give overflow errors). r = m%a; k = seed/q; seed = a*(seed - k*q) - k*r; if (seed<0) { seed = seed + m; } return (float)seed / (float)m; }