Random Number Generator Recommendation

12 downloads 0 Views 452KB Size Report
The generation of uniform pseudo-random numbers between 0 and 1 is ... This is referred to as the bitwise behavior of the random number generator (RNG).
Random Number Generator Recommendation by Charles N. Zeeb and Patrick J. Burns Colorado State University Department of Mechanical Engineering Fort Collins, CO 80523 [email protected] and [email protected] (970) 491-7479 and (970) 491-5778

The generation of uniform pseudo-random numbers between 0 and 1 is important in many numerical simulations. The purpose of this report is to explore the best generator(s) of such random numbers in terms of statistical properties and speed. While attempting to find the best generator in general, the specific goal of this report is to find the best generator for Latin hypercube sampling [Iman and Shortencarier, 1984]. This report starts by giving some background on uniform random numbers and the three basic types of generators: linear congruential, lagged Fibonacci, and combined. Next, some general coding issues are covered. Then some random number generators, the MCNP linear congruential generator and some implementations of the addition lagged Fibonacci generators, are tested in several ways to assess the quality of the random numbers they generate. This is followed by timing results are given for several different generators. Finally, conclusions and recommendations are given.

1

Uniform Random Numbers and Random Bits

For most applications, the type of random sequence desired is Rn, a sequence of random real numbers uniformly distributed between 0 and 1. Due to the finite accuracy to which a computer can represent a real number, what is actually generated is a series of integers, Xn, uniformly distributed between 0 and m, the modulus. The random numbers, Rn, are then generated by the following formula: R n = Xn ⁄ m

(1)

m is usually 2N where N is often related to the number of binary bits in the integer variable type used to store Xn. For some applications, not only is it important for the sequence Rn to be random, but also for the sequences of each individual bit (binary digit) representing Xn to be random. This is referred to as the bitwise behavior of the random number generator (RNG).

2

Basic Generators

There are three general classes of pseudo-random number generators in popular use: linear congruential generators, lagged Fibonacci generators, and combination genera-

1

tors. All three will be discussed briefly below. There are several good overviews of this topic. The classic reference is Knuth [1981]. A particularly brief overview is given by Marsaglia [1985]. More complete overviews are given by Anderson [1990] and Brent [1992]. 2.1

Linear Congruential Generators

Linear congruential generators, also referred to as multiplicative generators, are very popular. They are also the oldest in common use, having been proposed by D. H. Lehmer in 1949 [Knuth, 1981]. Besides Knuth [1981], a particularly good source of information is Park and Miller [1988]. A linear congruential generator is determined by the following four integer values [Knuth, 1981]: m the modulus a the multiplier c the increment X0 the starting value The sequence is given by:

m>0 0≤a 0 2. m = 2N, c = 0 3. m = prime, c = 0. While class 2 is the fastest and easiest to implement, Class 3 is the only class for which the individual low order bits are random. Choice of the values of m, a, and c are critical. While some choices give good results, others give horrible results [Knuth, 1981; Park and Miller, 1988; Brent, 1992]. There are three particular problems with LCG’s. One serious flaw is that plotting ntuples of consecutive random numbers generated by the LCG display an obvious lattice structure. An example of this is given in Figure 1 which shows the result of plotting the consecutive pairs of random numbers (Rn, Rn+1) over the full period of the RNG LCG(16807, 0, 231 - 1, 1), where we “zoom in” on only the portion of the unit square in the (0,0.0010) X (0.001,0) corner or 1/1,000,000 of the total area. This generator is the “minimal standard” suggested by Park and Miller [1988]. “Although the row spacing for this generator is obviously not optimal, this particular one is popular for its universal portability and its satisfactory performance in many applications” [Burns and Pryor, 2

0.0010

0.0008

Rn+1

0.0006

0.0004

0.0002

0.0000 0.0000

0.0002

0.0004

0.0006

0.0008

0.0010

Rn Figure 1 Lattice Structure for LCG (16807, 0, 231 - 1, 1) 1995]. More details about this particular LCG generator are given by Press et al. [1992]. Other examples of the lattice structure of linear congruential generators are given by Anderson [1990] and Burns and Pryor [1995]. Another problem is that the periods of LCG generators are often too short. The period of the LCG depends on m. The maximum period is m for class 1; 2N-2 for class 2; and m 1 for class 3 [Anderson, 1990]. For easy implementation on 32 byte machines, m is usually set to 231 or less. (The maximum value of a signed 32 bit integer is 231 - 1 ≅ 2 billion). Many problems require many more random numbers than this. Furthermore, these types of generators are known to have long-range correlations, especially for intervals which are a power of 2 [Coddington, 1992]. Still, the general consensus is that while LCG generators are not the best random number generators, they are still very good, provided the constants are chosen carefully. Furthermore, there has been more theoretical work done on these types of generators than any other type, so for problems where the above two limitations do not matter, LCG generators are often the generator of choice. Knuth [1981] considers it “the “nicest” and “simplest” random number generator for the machine language of most computers.” Marsaglia [1985] states that “They are the most widely used RNG’s, and they work

3

remarkably well for most purposes.” It should be noted however that in that very same paper, the LCG fail three of the eight stringent tests in his DIEHARD test battery, particularly those designed to show lattice effects. Anderson [1990] states: “Multiplicative congruential generators are adequate to good for many applications. They are not acceptable (because of the lattice effect) for high-dimensional work. They can be very good if speed is the major consideration. Prime moduli are best. However moduli of the form 2n are faster on binary computers when the code is written in assembly language.” Coddington [1992] states: “LCG’s have the advantage that we have a relatively good (although still limited) theoretical understanding of their randomness properties. They are known to be defective, but their defects are fairly well understood (for example, the lattice structure of an LCG can be determined analytically using the spectral test), and in practice they work quite well.” The LHS program [Iman and Shortencarier, 1984] currently uses a LCG(519, 0, 248, X0) [Hendricks, 1991]. This generator is also called the MCNP generator in this report because of its purported implementation in the Los Alamos Monte Carlo code, MCNP. Particularly noteworthy is the large value of m (248). The LCG has a maximum period of 246 ≅ 7.04X1012 if the initial seed is odd [Anderson, 1990; Hendricks, 1991]. The price paid for this large period is that it takes a very slow and complicated algorithm to implement this LCG on 32 bit machines. This will be shown clearly in the time trials below. The implementation used in the LHSTEST program [Romero, 1997] is designed only for 32 bit machines. The same algorithm is used even if 48 bit integers are available. It should also be noted that there is an error in the implementation of the generator that limits its portability. The generator code assumes that the values of variables are saved between subroutine calls and does not use the FORTRAN SAVE statement to ensure this. On machines/compilers where this is not true, such as the Macintosh Absoft FORTRAN compiler and at least some versions of the Intel Paragon, each call of the LCG generates the same value. Furthermore, the version of the generator used by LHSTEST is older than the one discussed in Hendricks’ article. It uses a different random number stride than the one proposed in that article. 2.2

Lagged Fibonacci Generators

2.2.1

Definition

From [Anderson, 1990]: “The general form of such generators is represented by LF [r, s, m, op; x{0…r-1}].

4

Here, 0

Suggest Documents