You are on page 1of 26

lgorithm to reproduce the same stream of random numbers on successive runs If the run is a

debug run notedperhaps by a parameter in the input lethe seed should be set to a constant initial
value such as a large prime number it should be oddas this will satisfy period conditions for any
modulusOtherwise the initial seed should be set to a
randomodd value Anderson recommends setting the initial seed X to the following integerX iyr #
imonth # iday # ihour #
imin # isec where the variables on the right hand side are the integer values of the date and
timeNote that the year is digits long ie the domain of iyr is However we have found it preferable
to introduce the maximum variation in the seed into the least signicant bits by using the second of
this century rather than the most signicant bitsErgowe prefer the followingX isec # imin # ihr #
iday # imon # iyr andto ensure X is odd in FORTRANx iorx Arithmetical Considerations Overow
and Negative Integers Consider performing the operation aXn # c mod m A large value of a is
desirable to provide su-cient randomnessA large value of m is also desiredso that the period is
kept longFor example on bit computers a and m are often bits long as the most signicant bit the
nd bit is generally used to indicate sign When two bit integers are multiplied together a possibly
bit integer resultsThusan overow almost always occursFortunately oating point multipliers and
software emulations thereof are designed to throw away the most signicant bits and retain the
least signicant bitsHowever if the result of the multiplication is so as to have the most signicant bit
the nd bit setthen the computer may treat this as a negative integerwhich is incompatible with
the algorithm above If this happens it occurs during one half of the multiply oper ationsthis
negative bit must be handled One strategy to overcome this is to use a bit mask to mask o the
most signicant bitIn this casea logical AND operation should be performed on the randomnegative
integer and the bit mask imaskzfffffff which is a leading followed by ones ie This has the eect of
the zeroing out the sign bitforcing the number to be positiveViz Remarks Table Random Sequence
of Example LCG
Random
Random
Random n Integer Xn Binary Xn Real Xn
1011011001 0000011111 0000011001 Xn = 730 MASK = 31 Xn = 25 IAND mod = (31 = 25 - 1)
Figure Modulus Operation with m using IAND xn iandxn imask Howevermasking works only
when m is a power of twoOtherwiseit destroys the sequence of numbers generatedand the theory
no longer appliesIn the general case unless the number resulting from the multiplication aXn ts in
the registerspecial coding must be doneAn approach to this is to perform remaindering by
decomposing the multiply operation into steps in which intermediate results are contained in bits
This is the strategy used by Park and Miller Park and Miller Note thatin the language Cone can
avoid the issue of the sign bit by using an unsigned integer Using Logical Masks to Perform
Modulo M Operations Generators where the modulus operation must be performed requiring an
integer division are more costly to implement than are those with moduli of M where the integer
division and remaindering can be accomplished much more e-cientlyWe illustrate this as follows
With a divisor of M after the multiplication of aXn the next seed is obtained simply by performing
a logical AND of Xn with a mask of M ones right justied see Figure This is an extremely e-cient
operation on binary computersMany computer languages have a bitwise AND intrinsic functionUse
of the AND operation also avoids the problem with negative integersdiscussed aboveA FORTRAN
implementation would look like xn iandxn mask Summary of LCG Properties Following Anderson
Anderson we summarize the salient features and the recommen dations for three widely used
types of linearcongruential random number generators Remarks Multiplicative congruential
generators are adequate to good for many applications They are not acceptable for
highdimensional work They can be very good if speed is a major consideration Prime moduli are
best However moduli of the form N are faster on binary computers Anderson m M c The full
period of m M is obtained if and only if a mod
and c is odd often chosen as Low order bits are not random m M c The maximum period of this
generator is M one quarter the modulus and is obtained if and only if a mod or a mod is
preferred and the initial seed is oddLow order bits are not random m p prime c or c The
maximum period of this generator is p and is obtained if and only if a is a primitive element
modulo pNote that there is always one integer which maps only to itself In the case of c X maps
to itselfLow order bits may or may not be random Park and Miller Park and Miller recommend the
followingportable generator m p
a and c The code randomfbased upon Park and Millers algorithmis supplied with this
packageNote thateven when c is non zerothe maximum period of this generator is still one less
than the modulus Exercise to establish all cycles in LCGs when m p a prime Consider
linearcongruential random number generators If c it is obvious that X is not a good candidate
integer for the initial seed because it maps to itselfIn fact if m pa prime then there is always a
number which maps to itself a constant sequence even if c Prove this by nding the integer which
maps to itselfand which does not appear in the full period sequence of length m for the following
linearcongruential generators a LCG X b LCG X c LCG X Exercise
exploring all possible sequences in simple LCGs For the linear congruential generato

You might also like