Random Numb ers

RN

Copyright C June Computational Science Education Pro ject

Remarks

Keywords Random pseudorandom linear congruential lagged

Fib onacci

List of prerequisites

some exp osure to sequences and series some abilitytowork in base arithmetic

helpful go o d background in FORTRAN or some other pro cedural language

List of computational metho ds

pseudorandom numb ers random numb ers linear congruential generators LCGs

lagged Fib onacci generators LFGs

List of architecturescomputers

any computer with a FORTRAN compiler and bitwise logical functions

List of co des supplied

ranlcfor linear congruential generator

ranlffor lagged Fib onacci generator

randomfor p ortable linear congruential generator after Park and Miller

getseedfor to generate initial seeds from the time and date implemented for

Unix and IBM PCs

Scop e

lectures dep ending up on depth of coverage

Notation Key

a integer multiplier a m

c integer constant c m

D length of needle in Buons needle exp eriment

F fraction of trials needle falls within ruled grid in Buons needle exp eriment

k lag in lagged Fib onacci generator k

lag in lagged Fib onacci generator k

M numb er of binary bits in m

m integer mo dulus m

N numb er of trials

N numb er of parallel pro cessors

p

P p erio d of generator

p a prime integer

R random real numb er R

n n

S spacing b etween grid in Buons needle exp eriment

th

X n random integer

n

Subscripts and Sup erscripts

denotes initial value or seed

th

j denotes j value

th

denotes value

th

k denotes k value

th

n denotes n value

Monte Carlo Algorithms and Random Numbers

In this chapter we discuss the application of Monte Carlo metho ds to scientic and engi

neering problems There are problems which can b e solved only by the Monte Carlo metho d

and there also are problems which are b est solved bytheMonte Carlo metho d However

the metho d is often referred to as the metho d of last resort as it is apt to consume large

computing resources As such it is a metho d ideally suitable for inclusion in a textb o ok

on computational science as Monte Carlo programs due to their nature of consuming vast

computing resources have historically had to b e executed up on the fastest computers avail

able at the time and employ the most advanced algorithms implemented with substantial

programming acumen

Here weprovide a brief history of the Monte Carlo metho d A go o d early work is that

of Buons needle problem Ross where in Buon a French mathematician

exp erimentally determined a value of by casting a needle on a ruled grid Many trials were

required to obtain go o d accuracy The ob jectiveofthework wasavalidation of statistical

theory as opp osed to Monte Carlo simulations typically applied to day where answers to

new physical problems are sought Lord Rayleigh Rayleigh even delved into this eld

near the turn of the century Courant Fredericks and Levy in showed how the metho d

could b e used to solve b oundary value problems Ensuing work Hurd illustrated that

Remarks

Needle Length, D S S > D

Obtain π After “Many” Trials

S

Figure Illustration of Buons Needle Problem

if the entire eld were sought Monte Carlo metho ds are far from comp etitive However

if the solution in a restricted region of space is sought sayatapoint then Monte Carlo

metho ds may b e used to great advantage as they may b e implemented in restricted regions

of space andor time

Exercise Buons Needle Problem to illustrate geometrically

some of the prop erties of random numb ers

a Write a program to determine the value of bynumerically casting a needle of length

D on a ruled grid with spacing S as shown in Figure You must rst determine a

random p osition relative ruled grid as shown in Figure You must rst determine a

random p osition relative to a ruled line then determine a random angle and thirdly

see whether the needle falls within the ruled lines You may nd it instructiveto

compute approximations for versus the numb er of trials eg N and

For SDyour answer for the fraction of time F the needle falls within the

grid should b e Kalos

D

F

S

b Verify the ab ove equation What is the corresp onding formula for SDWarning

there is considerable eort involved in this derivation

c Using a needle and a piece of pap er up on whichyou have placed a ruled grid p erform

aphysical exp eriment to determine using the ab ove equation Perform rst then

and then trials Comment on the accuracy of these results relativetothe

accuracy of the results from the numerical exp eriment Discuss reasons for any bias

apparent in the results

Enrico Fermi in the s used Monte Carlo in the calculation of neutron diusion and

later designed the Fermiac a Monte Carlo mechanical device used in the calculation of crit

icalityinnuclear reactors In the s a formal foundation for the Monte Carlo metho d

was develop ed byvon Neumann who established the mathematical basis for probability

density functions PDFs inverse cumulative distribution functions CDFs and pseudo

random numb er generators The work was done in collab oration with who

realized the imp ortance of the digital computer in the implementation of the approach The

collab oration resulted from work on the Manhattan pro ject where the ENIACwas employed

in the calculation of yield Ulam et al Ulam and Metrop olis Eckhard

Metrop olis

Individuals in the IBM corp oration were pioneers in the eld of random numb er gen

eration p erhaps b ecause they were rst engaged in it due to their participation in the

Manhattan pro ject where Richard Feynman then directed their computing op erations a

fascinating exp osition of their approach to p erforming largescale computing involving a

parallel approach exists in Richard Feynmans Surely Youre Joking Mr Feynman It

is interesting to note the extremely primitive computing environments in existence at that

time and the challenges this presented to researchers see Hurd

Uses of Monte Carlo metho ds have b een manyandvaried since that time However due

to computer limitations the metho d has not yet fully lived up to its p otential as discussed

by Metrop olis Metrop olis Indeed this is reected in the stages the metho d has

undergone in the elds of engineering In the late s and s the metho d was tested

in a variety of engineering elds Meuller Ehrlich Polgar and Howell

Ha jiSheikh and Chandler et al At that time even simple problems were

computeb ound The metho d has since b een extended to more complex problems Howell

Mo dest Maltby a and b Maltby and Burns and Cro ckett

et al Since then attention was fo cused up on muchneeded convergence enhancement

pro cedures Kahn and Marshall Emery and Carson Burghart and Stevens

Lanore Shamsunder et al Zinsmeister and Sawyer Larsen and Howell

Many complex problems remained intractable through the seventies

With the advent of highsp eed sup ercomputers the eld has received increased attention

particularly with parallel algorithms whichhavemuch higher execution rates In his PhD

dissertation Brown intro duced the concept of the event step Brown enabling

ecientvectorization of Monte Carlo algorithms where the particles do not interact This

approachwas later successfully exploited by several investigators Martin et al Martin et

al rep orted sp eedups of a factor of ve on an IBM with vector units Nearly linear

sp eedup was rep orted Sequent Computer Systems on a parallel architecture for photon

tracing Bobrowicz et al Bobrowicz et al a and b obtained sp eedup factors of

from ve to eight in an algorithm where particles are accumulated in queues until ecient

vector lengths are obtained allowing physics algorithms such as the Los Alamos b enchmark

GAMTEB to b e eectively vectorized Burns et al Suchadvanced co ding techniques

Remarks

have enabled much bigger problems to b e attacked with improved accuracyHowever there

are still a host of problems which remain intractable even with an eectively vectorized

algorithm

Moreover some imp ediments to eectivevectorization have b een identied and analyzed

Zhong and Kalos Zhong and Kalos analyzed the straggler problem where few

particles p ersist for manyevent steps inhibiting p erformance due to the overhead incurred

where vectors are short Pryor and Burns Pryor and Burns Burns and Pryor

rep orted for one Monte Carlo problem where particles interact sp eedups on the order of

half of those observed where the particles do not interact alb eit with a vectorized algorithm

of greatly increased complexity The same problem has b een attacked with dierentphysics

McDonald and Bagano Dagum in a more ecient algorithm

Go o d general references on Monte Carlo ab ound Beckenback Hammersly and

Handscomb Schreider Kleinjnen Rub enstein Binder Ha ji

Sheikh in the literature Most haveadistinctbent usually either statistics or physics

Unfortunately there is a dearth involving largescale engineering applications

No tutorial on largescale Monte Carlo simulation can b e complete without a discussion

of pseudorandom numb er generators Where billions of random numb ers are required it

is essential that the generator b e of long p erio d have go o d statistical prop erties and b e

vectorizable On bit machines these criteria are usually satised with a multiplicative

generator if the constants are carefully chosen Kalos and Whitlo ck Indeed wehave

come a long way from the early days of random numb er generators Knuth For



example Crays FORTRAN callable generator ranf has a cycle length of and is very

ecientvectorized with lowoverhead

We summarize this intro duction with our impression of the present status of Monte Carlo

surface to surface simulation All the pieces of the puzzle havejustcomeinto conuence

for largescale Monte Carlo analysis First sup ercomputers are nowsuciently p owerful

to enable the simulation of very large engineering and physics systems involving thousands

of surfaces and billions of particle emissions Secondly a comprehensiveformulation for

material prop erties exists in the aggregate of several mo dels Thirdly an estimate of the

numb er of trials required to achieve a sp ecied level of accuracy is now obtainable prior

to execution This makes p ossible a formulation whichallows the numb er of emissions to

evolve dynamically as the simulation pro ceeds Finallyanumber of investigators have

eectively vectorized diverse Monte Carlo transp ort algorithms with a sucient base to

establish a synthesized approach Wenoweven have a quantitative mo del which allows the

assessment of the degree of parallelism and the amountofoverhead required Moreover with

the emergence of lagged Fib onacci generators parallelization at any granularity app ears to

b e easily implemented and robust With this as a p oint of departure wenowembark up on

a discussion of random numb er generators up on which all Monte Carlo metho ds rely

Intro duction to Pseudorandom Numbers

Anyone who considers arithmetical methods of producing random digits is of

course in a state of sin Johnvon Neumann

Anyone who has not seen the above quotation in at least places is probably

not very old DVPryor

In this section we shall explore p ossible reasons for von Neumanns curious statement

This statement is curious for two particular reasons arithmetic metho ds are widely used

on all computers to generate random numb ers and himself devised

such metho ds for generating random numb ers Von Neumann probably made the ab ove

statement in reference to the fact that there are manyways to go amiss when so doing Our

goal in this section is through a series of example problems to illustrate various prop erties

of random numb er generators With a visceral understanding of the generation pro cess and

a solid foundation of cause and eect illustrated through examples it is our hop e that this

may help prevent straying into unacceptable techniques

Indeed all random numb er generators are based up on sp ecic mathematical algorithms

which are rep eatable and sequential As such the numb ers are just pseudorandom Here for

simplicitywe shall term them just random numb ers sub ject to this realization Formally

Truly random is dened as exhibiting true randomness such as the time b etween

tics from a Geiger counter exp osed to a radioactiveelement

Pseudorandom is dened as having the appearance of randomness but nevertheless

exhibiting a sp ecic rep eatable pattern

Quasirandom is dened as lling the solution space sequentially in fact these sequences

are not at all random they are just comprehensive at a preset level of granularity

For example consider the integer space One quasirandom sequence which

lls that space is Another is Yet a third is

Pseudorandom sequences whichwould ll the space

are pseudorandom p ermutations of this set they contain the same numb ers but in a

dierent random order

As Monte Carlo simulations have b een develop ed on computers since their inception

metho ds of generating and dealing with random numb ers are fairly well established As

literally millions or even billions of random numb ers are required in a largescale simulation

the pro cess should ideally b e very ecient Indeed manyrandomnumb er generator routines

are written in assembly language for this reason Sometimes random numb er routines are

even put in line to avoid the overhead asso ciated with subroutine calls Often on vector

andor parallel computers blo cks of random numb ers are generated to amortize over many

random numb ers the overhead asso ciated with the generation of one random numb er It

is noteworthythattheFORTRAN ANSI standard is the rst one to identify a random

numb er generator in the ANSI sp ecication of the language Hitherto it has existed only as

an extension to the FORTRAN standard

Remarks

We shall not provide a comprehensive treatise on random numb ers There are many good

references which already do so Knuth Knuth is the denitive reference although it

may b e a bit to o abstract for the typical computational scientist Some implementations of

algorithms of which at large scale some are go o d and some are not so go o d can b e found

in Numerical Recipes Press et al Perhaps the most accessible and lucid exp osition

is that of Anderson Anderson who presents some excellent illustrative graphics an

approachwhichweemulate to illustrate the prop erties of some random numb er generators

Also our fo cus is up on eciency and eectiveness when using random numb er generators

in largescale computations

Random number generators should not be chosen at random Donald Knuth

Consistent with the previous citation we advise a mo dicum of caution in the use of

pseudorandom numb ers esp ecially in largescale problems There is an interesting anecdote

from Knuth who went to great lengths to implement what he thoughtwas to b e a sup erior

random numb er generator However up on testing it was found to pro duce very p o or random

numb ers illustrating that it is easy for even the exp erts a priori to misinterpret qualityThe

following comments derive from painful p ersonal exp eriences of one of the authors

When problems arise with random numb er generators they are exceedingly dicult

to isolate Often the problem can b e isolated only by replacing the existing random

numb er generator with one which is denitely sup erior although p erhaps muchslower

Tomake a selection of a sup erior generator requires b oth knowledge of the generator

currently in use and sometimes requires indepth knowledge of the prop erties of general

random numb er generators

Problems rarely o ccur when solving small test problems those for which analytical

or exp erimental answers are known Instead problems arise in large scale substan

tial examples involving p erhaps millions or even billions of random numb ers where

debugging is dicult due to the massiveamount of data

Finally in largescale problems where one is p orting a scalar to a vector or massively

parallel algorithm the random numb ers usually are accessed in a dierent order Thus

it is sometimes not p ossible to duplicate the run exactly The results converge only

asymptoticallyasthenumb er of trials increases

Thus when problems o ccur it is very dicult to isolate the problem to the random

numb er generator b ecause one tends to trace program execution an event step at a time

and it is only in aggregate over manyrandomnumb ers that the b ehavior of the random

numb er generator is awed In eect one loses sight of the forest for all of the trees

Typically in desp eration and as a last resort after manydays of debugging one changes the

random numb er generator and voila the problem disapp ears

The truly cautious researcher assesses dierent random numb er generators as the contin

uum analyst makes renements to a grid b etter and b etter random n umb er generators are

employed until the answers are indep endent of the random numb er generator This is rarely

if ever done in practice Waxing philosophical one wonders what number of Monte Carlo

simulations mayhave b een p erformed where the answers may in fact b e incorrect but not

grossly incorrect due to a aw inherent in the random numb er generator used Traditionally

wecavalierly accept the random numb er generator on the architecture of interest Fortu

nately due to the early and well publicized mistakes made using random numb er generators

their prop erties were thoroughly investigated by the mathematics community primarily in

the s Most of the random numb er generators in use to daywere designed with cog

nizance of past pitfalls and are adequate for almost all applications However we conclude

this section with a rm caveat emptor

Desirable Prop erties

When p erforming Monte Carlo simulation we use random numb ers to determine at

tributes such as outgoing direction energy etc for launched particles and interactions

of particles with the medium Viewing this pro cess physically the following prop erties are

desirable

The attributes of particles should not b e correlated That is the attributes of each

particle should b e indep endent of those attributes of any other particle

The attributes of particles should b e able to ll the entire attribute space in a manner

which is consistent with the physics For example if we are launching particles into a

hemispherical space ab ove a surface then we should b e able to approach completely

lling the hemisphere with outgoing directions as we approach an innite number of

particles launched At the very least holes or sparseness in the outgoing directions

should not aect the answers signicantly Also if we are sampling from an energy

distribution with an increasing numb er of particles we should b e able to duplicate

the energy distribution b etter and b etter until our simulated distribution is go o d

enough

Mathematically sp eaking the sequence of random numb ers used to eect a Monte Carlo

mo del should p ossess the following prop erties

Uncorrelated Sequences The sequences of random numb ers should b e serially uncorre

lated This means that any subsequence of random numb ers should not b e correlated

with any other subsequence of random numb ers Most esp ecially ntuples of random

numb ers should b e indep endent of one another For example if we are using the ran

dom numb er generator to generate outgoing directions so as to ll the hemispherical

space ab ovea point or area we should generate no unacceptable geometrical patterns in the distribution of outgoing directions

Remarks

Long Perio d The generator should b e of long period ideally the generator should not

rep eat practically the rep etition should o ccur only after the generation of a very large

set of random numb ers More explanation is provided b elow

Uniformity The sequence of random numb ers should b e uniform and unbiased That

is equal fractions of random numb ers should fall into equal areas in space For

example if random numb ers on are to b e generated it would b e p o or practice

were more than half to fall into presuming the sample size is suciently large

Often when there is a lack of uniformity there are ntuples of random numb ers which

are correlated In this case the space might b e lled in a denite easily observable

pattern Thus the prop erties of uniformity and uncorrelated sequences are lo osely

related

Eciency The generator should b e ecient In particular the generator used on vector

machines should b e vectorizable with lowoverhead On massively parallel archi

tectures the pro cessors should not havetocommunicate among themselves except

p erhaps during initialization This is not generally a signicant issue With minimal

eort random numb er generators can b e implementedinahighlevel language suchas

CorFORTRAN and b e observed to consume well less than of overall CPU time

over a large suite of applications

Exercise To illustrate the mean and variance of uniformly

distributed random numbers

For uniformly distributed real random numbers R on the average value should b e

n

Note that to compute the mean and variance we can order the random numbers in

any fashion as b oth of these computations are indep endent of order It is useful to cho ose

to order the real random numb ers in ascending order and approximate the distribution in

the form of a continuous variable RVerify by direct integration that the average R and



the variance should b e and resp ectively as follows

Z



R RdR



Z



 

R dR



The Random Number Cycle

Pursuant to the ab ove discussion it is useful to present pseudorandom sequences in the

context of their cyclical structure Almost all random numb er generators have as their basis

a sequence of pseudorandom integers there are exceptions The integers or xed p oint

numb ers are manipulated arithmetically to yield oating p oint or real numb ers The

Start Finish ● Clockwise ● ● Traversal

● ●

● ●

● ●

● ●

Figure Illustration of Random Numb er Cycle

random numb er cycle can b e presented in terms of either integers or real numb ers Here for

claritywe conne ourselves to integers

In Figure we illustrate a random numb er cycle representing a sequence of integers

Each black dot represents a distinct integer Our convention will b e to start at the top of the

cycle ie oclo ck traverse the cycle clo ckwise and nish at the integer just to the left

of the start The nature of the cycle illustrates that the sequence has a nite number of

integers the sequence gets traversed in a particular order and the sequence rep eats

if the p erio d of the generator is exceeded ie the cycle can b e traversed more than once

Furthermore the integers need not b e distinct that is they may rep eat We shall address

this p oint subsequently These all are prop erties of pseudorandom sequences of integers In

subsequent sections we shall illustrate these asp ects with sp ecic examples

Linear Congruential Generators

Approach

We b egin by discussing the linear congruential generator the one most commonly used for

generating random integers

X aX c mo d m

n n

Here we generate the next random integer X using the previous random integer X

n n

the integer constants a and c and the integer mo dulus m After the integer aX c is

n

generated mo dulo arithmetic using the mo dulus m is p erformed to yield the new random

integer X

n

Remarks

To get started the algorithm requires an initial seed X whichmust b e provided



by some means we shall discuss this later The entire sequence is characterized bythe

multiplier a the additive constant c the mo dulus m and the initial seed X Following



Anderson Anderson we therefore refer to the sequence generated as LC Ga c m X



which completely determines the sequence Here LCG denotes a Linear Congruential Gen

erator

The app earance of randomness is provided by p erforming mo dulo arithmetic or remain

dering For example the nonnegativeintegers mo dulo are

Note that the next result X dep ends up on only the previous integer X Thisisachar

n n

acteristic of linear congruential generators which minimizes storage requirements but at the

same time imp oses restrictions on the p erio d

With X determined we generate a corresp onding real numb er as follows

n

X X

n n

or R R

n n

oat m oat m

When dividing by mthe R values are then distributed on If we desire R to b e

n n

distributed on then wewould divide bym We desire uniformity where any

particular R is just as likely to app ear as any other R andtheaverage of the R is very

n n n

close to

Example LCG

Let us consider a simple example with a c m and X The sequence of



pseudorandom integers generated by this algorithm is

In Figure we illustrate the random numb er cycle for this generator We immediately

observe four features

The p erio d the number of integers b efore the sequence rep eats P is exactly equal

to the mo dulus m When the next result dep ends up on only the previous integer the

longest p erio d p ossible is P m In the current example with a mo dulus of the

mo d op eration generates integer results from to inclusive Thus for m this

sequence is of long p erio d the longest p ossible and uniform it completely lls the

 M

space of integers from Note that the p erio d is exactly equal to ie where

M is the base log of the mo dulus

This particular sequence exhibits throughout its p erio d the pattern of alternating o dd

and even integers It is frequently instructive to view this sequence in binary and then

after p erforming a real division by m to result in random real numb ers see Table

It is readily apparent that the sequence is serially correlated In fact note that the

rightmost binary digit exhibits the regular pattern Due to this lackof

Initial Seed

● ● ● Clockwise 1 Traversal ● 0 6 ● 3 15 ● ● 10 12

● 5 13 ●

● 4 2 ●

7 11 ● ● 14 8 ● 9 ●

Figure Random Numb er Cycle for Example LC

randomness the X values should not b e used as random digits esp ecially the right

n

most digits of X In fact this lack of randomness results from using a p ower of two

n

for m Despite this a mo dulus whichisa power of two is often used for this renders

the pro cess of p erforming the mo dulus op eration very ecient Moreover the real

numb ers generated from the integer sequence are generally suciently random in the

higher order most signicant bits to b e used in many application co des

Next we infer the following Because eachrandominteger results from the previous

integer alone selecting any initial seed from to would just cyclically shift the

ab ove sequence We could b egin anywhere on the random numbercycleandwewould

pro ceed clo ckwise around the random numb er cycle from that starting p oint Thus all

that a dierentchoice of the initial seed do es is shift the starting p oint in the sequence

already determined by a c and m

Finallywe note that the average of the real numb ers is and the variance is

The departure of these values from the ideal ones of and is

due to the short p erio d of this sequence and the rather coarse resolution of the generated

real numb ers These conditions of average and variance approaching the theoretical

values are necessary but not sucient conditions for a go o d random numb er generator

Let us exercise the co de ranlcf for additional parameters In so doing we shall cho ose

parameters that illustrate sp ecic asp ects of linear congruential generators First we take

the case of c This is termed a multiplicative congruential random numb er generator

Remarks

Table Random Sequence of Example LC G

Random Random Random

n Integer X Binary X Real X

n n n

Average

Variance

X aX mo d m

n n

The case c is termed a mixedcongruential random numb er generator

Example LCG

As illustrated in Figure we obtain the sequence

Note that wenowhave a p erio d P of only this is the mo dulus In fact when

M  M 

m is a p ower of here and c the maximum p erio d is Here again

wenotethatthelow order bits are not random In fact the two least signicant bits

Initial Seed

● Clockwise 1 Traversal

● 13 5 ●

9

Figure Random Numb er Cycle for Example LCG

are constant always and the most signicant bits are quasirandom exhibiting the

pattern and

Also the sequence is correlated as all successiveintegers dier by from their prede

cessors

At coarse granularity the sequence is uniform For example if we divide equally

into quarter segments then exactly one random numb er falls into each segment

and However at ner granularity this uniformity

breaks down consider dividing up the domain into equal segments for example

Here wehave a mismatchbetween the random numb er generator we are using and the

scale or granularity of our problem the RNG is incapable of pro ducing random

numb ers which ll our attribute space here our bins There are two separate

issues to consider here The least troublesome is the nite precision existing in all

computers which results in a roundo error to the precision with whichintegers can

b e represented or with which the oating p oint divide is accomplished More serious is

the second issue the interaction of the sequence of random numb ers pro duced by our

generator with our application This interaction is particularly troublesome when our

application requires ntuples of random numb ers instead of just one random number

at a time Later we shall illustrate this eect by displaying plots of individual p oints

generated using various RNGs In some applications several random numb ers are

needed at a time For example when tracing particles among surfaces three random

numb ers are required to obtain outgoing direction one for outgoing azimuthal angle

and one for outgoing p olar angle and energy usually only one is required to obtain

Remarks

Table Random Sequence of Example LC G

Random Random Random

n Integer X Binary X Real X

n n n

Average

Variance

the energy

Example LCG

We obtain the sequence

Table provides the sequence throughout the p erio d Here b ecause we use a prime number

as the divisor for the mo dulus op eration and c we obtain a p erio d one less than mo dulus

is not p ossible as it maps to itself so we obtain a p erio d of Indeed when m p

a prime the maximum p erio d P is m even if c Thus for linear congruential

max

generators with a prime mo dulus using a nonzero c do es not increase the p erio d

Here the low order bits while not exhibiting a discernible pattern do not app ear as

random as one might exp ect Indeed as is shown in Altman Altman the bitwise

randomnes prop erties of LCGs should b e considered on a case by case basis He provides

examples of LCGs with prime mo duli that fail bitwise testing but p oints out for example



that LC G X do es pass the bitwise randomness test



Exercise to illustrate the generation of random numb ers using

LCG and to partition the random real

numb ers into bins

Obtain and edit the linear congruential program ranlcf Supply the additional co de to

compute the average and the variance For sample sizes of and run

the co de and plot the errors in the average and variance versus sample size Discuss how

the error in these quantities varies with the sample size Use the following parameters



a c and m

Initial Seed

Now we address establishing the initial seed When debugging it is imp ortant to implement

the algorithm to repro duce the same stream of random numb ers on successive runs If the

run is a debug run noted p erhaps by a parameter in the input le the seed should b e set

to a constant initial value such as a large prime numb er it should b e o dd as this will satisfy

p erio d conditions for any mo dulus Otherwise the initial seed should b e set to a random

odd value Anderson recommends setting the initial seed X to the following integer



X iy r imonth iday ihour imin isec



where the variables on the righthand side are the integer values of the date and time Note

that the year is digits long ie the domain of iy r is However wehave found it

preferable to intro duce the maximum variation in the seed into the least signicant bits by

using the second of this century rather than the most signicant bits Ergo we prefer the

following

X isec imin ihr iday imon iy r



and to ensure X is o dd in FORTRAN



x iorx

Arithmetical Considerations

Overow and NegativeIntegers

Consider p erforming the op eration aX c mo d m A large value of a is desirable to

n

provide sucient randomness A large value of m is also desired so that the p erio d is

kept long For 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 twobitintegers

are multiplied together a p ossibly bit integer results Thus an overow almost always

o ccurs Fortunately oating p ointmultipliers and software emulations thereof are designed

to throwaway the most signicant bits and retain the least signicant bits

However if the result of the multiplication is so as to have the most signicant bit the

nd bit set then the computer may treat this as a negativeinteger which is incompatible

with the algorithm ab ove If this happ ens it o ccurs during onehalf of the multiply op er

ations this negativebitmust b e handled One strategy to overcome this is to use a bit

mask to mask o the most signicant bit In this case a logical AND op eration should b e

p erformed on the random negativeinteger and the bit mask imaskzfffffffwhich

is a leading followed by ones ie This has the



eect of the zeroing out the sign bit forcing the numb er to b e p ositive Viz

Remarks

Table Random Sequence of Example LC G

Random Random Random

n Integer X Binary X Real X

n n n

1011011001 Xn = 730

IAND mod

MASK = 31 0000011111 (31 = 25 - 1)

=

0000011001 Xn = 25



Figure Mo dulus Op eration with m using IAND

xn iandxn imask

However masking works only when m isapower of two Otherwise it destroys the

sequence of numb ers generated and the theory no longer applies In the general case unless

the numb er resulting from the multiplication aX ts in the register sp ecial co ding must

n

b e done An approach to this is to p erform remaindering by decomp osing the multiply

op eration into steps in whichintermediate results are contained in bits This is the

strategy used byPark and Miller Park and Miller Note that in the language C one

can avoid the issue of the sign bit by using an unsigned integer

M

Using Logical Masks to Perform Mo dulo  Op erations

Generators where the mo dulus op eration must b e p erformed requiring an integer division

M

are more costly to implement than are those with mo duli of where the integer division

and remaindering can b e accomplished much more ecientlyWe illustrate this as follows

M

With a divisor of after the multiplication of aX the next seed is obtained simply by

n

p erforming a logical AND of X with a mask of M ones rightjustied see Figure

n

This is an extremely ecient op eration on binary computers Many computer languages

have a bitwise AND intrinsic function Use of the AND op eration also avoids the problem

with negativeintegers discussed ab ove A FORTRAN implementation would lo ok like

xn iandxn mask

Summary of LCG Prop erties

Following Anderson Anderson we summarize the salient features and the recommen

dations for three widely used typ es of linear congruential random numb er generators

Remarks

Multiplicative congruential generators are adequate to good for many appli

cations They are not acceptable for highdimensional work They can be very

goodifspeed is a major consideration Prime moduli arebest However moduli

N

of the form are faster on binary computers Anderson

M

m c

M

The full p erio d of m is obtained if and only if a mo d and c is o dd

often chosen as Loworder bits are not random

M

m c

M 

The maximum p erio d of this generator is onequarter the mo dulus and is

obtained if and only if a mo d or a mo d is preferred and the

initial seed is o dd Loworder bits are not random

m p primec or c

The maximum p erio d of this generator is p and is obtained if and only if a is a

primitive elementmodulo p Note that there is always one integer which maps only

to itself In the case of c X maps to itself Loworder bits mayormay



not b e random Park and Miller Park and Miller recommend the following



p ortable generator m p a and c The

co de randomf based up on Park and Millers algorithm is supplied with this package

Note that even when c is nonzero the maximum p erio d of this generator is still one

less than the mo dulus

Exercise to establish all cycles in LCGs when m  p a prime

Consider linear congruential random numb er generators If c itisobvious that X



is not a go o d candidate integer for the initial seed b ecause it maps to itself In fact if m p

a 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 itself and which do es not app ear in

the full p erio d sequence of length m for the following linear congruential generators

a LC G X



b LC G X



c LC G X



Exercise exploring all p ossible sequences in simple LCGs

For the linear congruential generator LC G X generate all the p ossible sequences



byvarying the initial seed from and Howmany indep endent ie not cyclical shifts of

one another sequences are there Is there a pattern observable in even versus o dd integers

Discuss p ossible reasons for this b ehavior

Exercise Crays LCG ranf

On a Cray sup ercomputer running UNICOS obtain the manualpageonitsintrinsic random

numb er generator bytyping at the UNICOS prompt man ranf ranfdoc Read the

le ranfdoc to see how to set and get the seed for ranfGiven that ranf is a multiplicative

congruential generator c using bits get the initial seed discuss it in terms of cycle

length Set the seed to and call ranf to obtain a new seed Get and print out the new

seed this new seed is the multiplier of the generator Discuss the multiplier in the context of

the cycle length Print out the rst ten random integers in decimal and hexadecimal format

and the corresp onding random real numb ers using an f format

Ntuple Generation With LCGs

Supp ose wewanted to cho ose random lo cations in the unit square for Monte Carlo trials in

an application program Avery easy waytocho ose these p oints is to select two random

values R and R in and cho osing the p ointR R as the currentpointofinterest

   

in the square More generallywewould generate a p ointby plotting R vs R whereR

n n n

and R are of course obtained by scaling successive outputs X and X of the generator

n n n

LC Ga c m X bymIfwe rep eat this pro cedure for a large numb er of trials wewould



like to exp ect that we will achieve a reasonably go o d covering of the unit square and in

a randomly ordered fashion Wewould b e suspicious of a generator that pro duced p oints

in the square that were say clustered in the b ottom half or p erhaps covered the square in

some clear order from left to right Many other forms of obvious nonrandom b ehavior would

b e equally unacceptable for most Monte Carlo applications A characteristic of LCGs is that

p oints selected in this way and plotted in the unit square b egin to form regularlo oking rows

or dotted lines that are easily discernible when enough p oints have b een plotted and when

viewed at the prop er scale Over the entire p erio d of an LCG if all consecutive pairs are

plotted then these rows ll in to b ecome evenly spaced b etween p oints

This constitutes one of the well known tests of randomness applied to pseudorandom

numb er generators the socalled sp ectral test the ob ject of which is to discover the b ehavior

of a generator when its ouputs are used to form ntuples The formal developmentofthis

family of tests is dicult and wewillnotcover it here the interested reader is referred

to Knuth but we will attempt to illustrate through several examples the meaning of the

concept as it applies to a dimensional setting and with linear congruential generators The

concepts presented are valid in higher dimensions as well

In Figure af we present a few examples to illustrate this b ehavior These examples

depict the result of generating all pairs of consecutivenumb ers in the p erio d of fullp erio d

LCGs For each mo dulus the plotted p oints show the eect of our choice of the multiplier

a In Figure a we show the set of p oints pro duced by scaling by the output of the

generator LC G Note that the p oints form more than one set of rows That is

there are several angles from which the p oints app ear to line up From some p ersp ectives

the rows are close together while from at least one p ersp ective the rows are rather far apart

Remarks

1.0

R(n+1) 0.5

0.0 0.0 0.5 1.0

R(n)

Figure a Pair Plot of LCG

1.0

R(n+1) 0.5

0.0 0.0 0.5 1.0

R(n)

Figure b Pair Plot of LCG

1.0

R(n+1) 0.5

0.0 0.0 0.5 1.0

R(n)

Figure c Pair Plot of LCG

1.0

R(n+1) 0.5

0.0 0.0 0.5 1.0

R(n)

Figure d Pair Plot of LCG

Remarks

1.0

R(n+1) 0.5

0.0 0.0 0.5 1.0

R(n)

Figure e Pair Plot of LCG

0.00050

R(n+1) 0.00025

0.00000 0.00000 0.00025 0.00050

R(n)

Figure f Pair Plot of LCG

The b est situation is to have the maximum spacing of the rows when viewed over all angles

as small as p ossible When the maximum spacing is large as it certainly is in Figure b

LC G then clearly the unit square is not well covered by this set of p oints and

the results of our simulation maybeadversely aected by this striping Figure c shows

the eect of a go o d choice of a LC G for which the maximum spacing b etween

rows is clearly smaller than it is in the rst two cases and where the p oints cover the unit

square with nearly optimal uniformitygiven that with this generator we can generate only

a small number of points

Figures ac are examples that all use the prime mo dulus m When m is a p ower

of two the other ma jor case to consider the situation is similar as depicted in Figures d

and e Recall that when m is a p ower of twoand c the full p erio d is m but only if



a modor a mo d Thus wecho ose m so that with p erio d we

plot almost the same numb er of p oints as we see in Figures ac Figure d illustrates

the result of using a go o d choice of a a mo d As in the prime mo dulus case

the plotted p oints form a uniform lo oking lattice But when we plot the p oints in Figure

e generated by taking a mo d we see that the pattern lo oks liketwo lattice

structures slightly oset from one another This double lattice distinguishes the a mod

case from the a mo d case and is the reason that a mo d is preferred even

though b oth sets of a values pro duce the same p erio d As an aside the two lattice structures

in the a mo d case come ab out from plotting separately the pairs b eginning with o dd

indices and those b eginning with even indices

The cases lo oked at so far are for mo duli chosen to b e small enough that we can plot the

fullp erio d results and still see a pattern with space b etween p oints In practice it would

b e fo olish to use a generator with such a short p erio d If we lo ok at an LCG with much

longer p erio d one that wemight use in a practical application we still see the latticelike



fullp erio d b ehavior Figure f shows the fullp erio d D result for LC G

where we zo om in on only the p ortion of the unit square in the X

corner or of the total area Although the row spacing for this generator is

obviously not optimal this particular one is p opular for its universal p ortability and its

satisfactory p erformance in many applications

Wehave shown only examples in which c Using a nonzero value for c when m

is prime has the eect of shifting the entire lattice structure to a new lo cation so that it

app ears oset from that of the c case The orientation and spacing of the lattice remain

the same except that one lattice p oint app ears absent this reects the fact that when m

is prime and c some nonzero value of X will succeed itself in the recursion formula of

the generator Actually there is a hole in the c lattice as well but it lies on one of

the axes and so do esnt disrupt the apparent regularity When m is a p ower of two and c

is nonzero recall that the p erio d of the generator is four times the p erio d of the c case

Here again the resulting lattice app ears to b e aligned the same as the c lattice with the

c lattice oset from a subset of the c lattice A further complication arises in the

case where m isapower of twoanda mo d here a nonzero value for c will pro duce

dierent osets for eachofthetwo distinct lattice structures relative to the c case The

Remarks

fact that the c case is slightly more complicated than the c case should not obscure

the basic theme of this section that the quality of an LGC used in generating ntuples is a

function of the multiplier a

The remarks given here with resp ect to D b ehavior of LCGs apply as well to higher

dimensions For example in three dimensions the co ordinate triples R R R formed

  

as ab ove will lie in distinct planes For ease of illustration wehave considered only D

examples and only in a qualitative sense The lattice spacing in anynumb er of dimensions

can b e treated quantitatively using metho ds detailed in Knuth VolTheinterested reader

is encouraged to pursue the topic further in this source Rememb er that the eects describ ed

here are fullp erio d prop erties of LCGs If only a tiny fraction of the p erio d is used the most

desirable way to use any pseudorandom generator then these eects may not b e noticed

If a user susp ects that the lattice structure of ntuple generation is aecting the outcome of

aMonte Carlo simulation then the b est p olicy is to try another generator of dierenttyp e

and compare the results

Exercise visualization of some p o or LCGs

Mo dify the ranlcf co de to output x y pairs of numbers as R R R R R R

     

etc Plot these discrete p oints for the following cases



a a c m p oints random numb ers



b a c m p oints random numb ers

Vectorization and Access via Multiple Pro cessors

LCGs

Many Monte Carlo applications havecharacteristics that make them easy to map onto com

puters having multiple pro cessors Some of these parallel implementations require little or no

interpro cessor communication such applications are called embarrassingly parallel and

are typically easy to co de on a parallel machine Others require frequent communication

and synchronization among pro cessors and in general are more dicult to write and debug

In developing any parallel Monte Carlo co de it is imp ortant to b e able to repro duce runs

exactly in order to trace program execution Pro cessors in MIMD machines are sub ject

to asynchronous and unbalanced external eects and are thus for all practical purp oses

imp ossible to keep aligned in time in a predictable way If the assumption is made that

random numb er generation from a single generator will o ccur across pro cessors in a certain

predictable order then that assumption will quite likely b e wrong A number of techniques

havebeendevelop ed that guarantee repro ducibilityinmultipro cessor settings and with var

ious typ es of Monte Carlo problems We will consider only simple extensions to our previous

discussion of LCGs but acknowledge that there are many approaches to parallel random

numb er generation in the literature The rst situation we address involves using LCGs in

a xed numb er of MIMD pro cesses where that number is known at the b eginning of a run

Supp ose we knowinadvance that wewillhave N indep endent pro cesses and that we

will need N indep endent streams of random numb ers Then the b est strategy for using an

LCG is to split its p erio d into nonoverlapping segments eachofwhich will b e accessed by

a dierent pro cess This amounts to nding N seeds which are known to b e far apart on

the cycle pro duced by the LCG To nd such seeds rst consider for c the LCG rule

successively applied

X aX mo d m

n n



X aX a X mo d m

n n n



X aX a X mo d m

n n n

k

X aX a X mo d m

nk nk n

Thus we can leap ahead k places of the p erio d bymultiplying the currentseedvalue by

k

a mo d mFor our purp oses wewould like N starting seeds spaced at roughly k PN

k

steps apart Since k is likely to b e quite large it is not practical to compute a one step at

a time Instead we compute an L long array dthepoweroftwopowers of a

d a





d d

 



d d

 



d d

L L

where d is the largest p oweroftwopower of a that is still smaller than k Ie L is the

L

integer part of the log base of aFor example assume that k very



small but big enough to showhowitworks Then

    

d a a a a a a a

and since then

    

a a a a a a d d d d d

    

k

Thus for any k a d for all i for which bit i in the basetwo representation of k is a one

i

k

Therefore we can leap ahead by k cycle steps with no more than log k multiplies Once a



is computed the N seeds can b e determined by the pro cedure

Cho ose seed 

Remarks

k

seed a seed mo d m

 

k

seed a seed mo d m

 

k

seed a seed mo d m

N N 

With these seeds each of the N pro cesses will generate random numb ers from nearly equally

spaced p oints on the cycle As long as no pro cess needs more than k random numb ers a

condition easily met for some applications then no overlap will o ccur Everything just said

for MIMD pro cesses applies equally well to SIMD programs where the numb er of random

numb er streams needed is usually known at run time

The development of the leap ahead technique just describ ed assumed that c in the

LCG rule For c Leap ahead can still b e accomplished in a similar way if one constructs

P

j

i

the log k long array of partial sums of the form s a where as b efore j isapower

 j

i 

of two The details are left as an exercise for the reader

The second and more dicult case to consider is when we do not know at the b eginning of

program execution howmany pro cesses generators we will need The splitting of pro cesses

in such programs are data driven and in most cases o ccur as the result of prior Monte Carlo

trials taken many steps earlier The problem is to spawn new LCG seeds in a way that is

b oth repro ducible and which yields indep endent new streams

Here we only mention a generalized approach that works within limits Further details

can b e found in Frederickson et al Consider an LGC with the prop ertythateach X

has two successors a left successor X and a right successor X These are generated

L R

as follows

LX X a X c mo d m and

L L L

RX X a X c mo d m

R R R

Figure shows the action of these op erations with resp ect to a starting seed X Taken sep



aratelytheX and X sequences are simple LCGs that traverse the set f m g

L R

in dierent order Alternatively and the metho d in which these generators are typically

used the X rule pro duces a pseudorandom leapahead for the X sequence thus deter

L R

ministically pro ducing a seed for a new spawned subsequence of the right cycle With

such a mechanism that uses only lo cal information from a pro cess repro ducibilitycanbe

established Frederickson gives a formula for the selection of the constants in the succes

sion rules that satises a particular indep endence criterion given some constraints The

interested reader is referred to Frederickson et al for further enlightenment

Exercise Vectorization of Crays LCG



Complete Exercise ab ove to determine the parameters a and m probably m of

Crays ranfDevelop a vectorized version of ranf by creating a vector of successive

multiples of the co ecient aFor a vector length of for example you will need a vector

  

of a a a a Race your vectorized version of ranf vs Crays ranf and see

Initial Seed Left Sequence x Right Sequences ● XL,n+1 = aL XL,n+ cL XR,n+1 = aR XR,n+ cR

L(x) ● ● R(x)

L2(x) ● ● RL(x) LR(x)●● R2(x)

● ● ●● ● ● ● ●

L3(x)RL2(x) LRL(x) R2L(x) L2R(x) RLR(x) LR2(x) R3(x)

● ● ● ● ●

● ● ● ● ●

● ● ● ● ●

Figure Tree Generated from OLeftOand ORightO Generator

how close you can come to their execution time To do this you will have to generate long

vectors of random numb ers and use them elsewhere in the co de Use second to obtain

elapsed CPU time

Exercise Leapahead in an LCG

For the LCG X aX c where c determinea andc so that X a X c

n n k k nk k n k

resulting in a leapahead of k places on the original LCG cycle

Lagged Fib onacci Generators

Lagged Fib onacci pseudorandom numb er generators have b ecome increasingly p opular in

recentyears These generators are so named b ecause of their similarity to the familiar

Remarks

Fib onacci sequence

dened by X X X

n n n

where the rst twovalues X and X must b e supplied This formula is generalized to give

 

a family of pseudorandom numb er generators of the form

X X X mo d m where k

n n nk

and where instead of two initial values initial values X X are needed in order

 

to compute the next sequence element In this expression the lags are k and so that

the currentvalue of X is determined by the value of Xkplaces ago and places ago In

M

addition for most applications of interest m is a p ower of two That is m This

typ e of pseudorandom numb er generator along with several others has b een extensively

tested for randomness prop erties by Marsaglia Marsaglia and has b een given high

marks The only deciency found was related to what he calls the Birthday Spacings test

for lowvalues of and k The interested reader is referred to Marsaglia

With prop er choice of k and the rst values of X the p erio d P of this generator

M 

is equal to Prop er choice of and k here means that the trinomial

k

x x is primitiveover the integers mo d The only condition on the rst values is

that at least one of them must b e o dd Using the notation LF G k M to indicate the lags

and the p ower of two mo dulus examples of two commonly used versions of these generators

are



LF G k M P



LF G k M P

Obviously the value of the mo dulus mdoesnotby itself limit the p erio d of the generator

as it do es in the case of an LCG Note also that lagged Fib onacci pseudorandom number

generation is computationally simple an integer add a logical AND to accomplish the mo d

M

op eration and the decrementing of two array p ointers are the only op erations required

to pro duce a new random number X Furthermore with a large enough value of k limited

vectorization can b e achieved The ma jor drawback in the case of this typ e of generator is

the fact that words of memory must b e kept current An LCG requires only one the last

value of X generated

Wenow lo ok at some of the theory of these generators with a view toward ensuring their

prop er use Conceptually a Fib onacci generator acts the same as a linear shift register and



if we set M so that m then wehave a binary linear shift register Figure is

a diagram of a particular binary linear shift register where k andevery X is

j

either a or a We will use this choice of and k in several examples in order to illustrate

in a nontrivial way some of the prop erties of this typ e of generator The arrows are there

to depict the motion of the shift register as it makes the transition from one state to the

next This is called advancing the register Twosuchadvances are shown in Figure

Addition mod 2

X(n-10) X(n-9) X(n-8) X(n-7) X(n-6) X(n-5) X(n-4) X(n-3) X(n-2) X(n-1)

bit 9 bit 8 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0

Figure State Transition Diagram for Binary Shift Register



If weset m equal to a higher p ower of two say m ieM then the singlebit

values of the X s in Figure will instead b e represented by M bits each Figure is a

diagram of the mo d linear shift register asso ciated with the equation



X X X mo d

n n n

In lo oking at this more general Fib onacci generator LF G two things are worth

noting

If we lo ok for a moment at only the least signicant bits of the elements in this register

that is only those bits in the b ottom row then we see that the b ehavior of this rowis

unaected by bits in the higher rows The contents of the b ottom row will therefore

b e indistinguishable from those of the binary shift register in Figure

This is no surprise when w e add twonumb ers the answer in the ones place do es

not dep end in anyway on the digits in the tens place here the twos place

If the b ottom row of bits is all zeros then no amount of register motion can pro duce

any ones there This is to say that if the rst values of X are all chosentobeeven

then there can b e no subsequent X values that are o dd when the register is advanced

In such a case the pseudorandom numb er generator will not b e of full p erio d The

p erio d can b e no larger and ma y b e smaller than P since all o dd numb ers will

b e excluded

Since the state transformation of the shift register contents is a linear op eration a matrix

equation describing it can b e given Continuing with the example of the long generator

Remarks

Addition mod 2

1 1 000001 11

1 0 1110 0 0 0 0

001 001 001 1

Figure XXX

if we dene x and A by

X

n

B C B C

X

n

B C B C

B C B C

X

B C B C

n

B C B C

B C B C

X

n

B C B C

B C B C

X

B C B C

n

x B C and A B C

B C B C

X

n

B C B C

B C B C

X

n

B C B C

B C B C

B C B C

X

n

B C B C

A A

X

n

X

n

Addition mod 16

m.s.b. word 0 word 1 word L-1

l.s.b.

X(n-10) X(n-9) X(n-8) X(n-7) X(n-6) X(n-5) X(n-4) X(n-3) X(n-2) X(n-1)

Figure Register Motion for LFG

then the action of the shift register can b e readily describ ed by the equation

M

x Ax mo d

n

n

where x is the entire vector after n time steps If the vector x has b een given some initial



n

set of values then wehave

x Ax

 



x Ax A x

  

 

x Ax A x A x

   

etc

and in general

n

x A x



n

P P

As an aside note that x A x x or A I the identitymatrix



P P

The ab ove equation gives a way to leap the generator ahead similar in fashion to the

leap ahead concept discussed for LCGs But such an op eration with a Fib onacci generator

Remarks

requires a matrixvector multiply involving at b est the precomputation of p ossibly many

multiples of A This precomputation may b e exp ensive even for small values of recall

that A is and may b e prohibitive for large values of Leaping a Fib onacci generator

ahead is therefore not recommended This do es not mean that such a generator cannot b e

split however Wenow lo ok at an ecient metho d for splitting a Fib onacci generator

Before explaining the splitting technique notice that the p erio d P of a prop erly con

M 

structed Fib onacci generator is Consider a given initial set of values of an

M bit long Fib onacci register This state is a particular M bitpatternintherect

angular register If the register is advanced P times the initial pattern will b e replaced by

P dierent patterns b efore the initial one reapp ears But the number of possibl e bit

M 

patterns in the register is anumb er far larger than P This tells us that there are

many P long cycles that are indep endent of one other and can b e generated from the same

 M 

LF G k M structure The numb er of such fullp erio d cycles is Mascagni et

al For example with the LF G generator of our previous example there



are separate fullp erio d cycles and a much smaller numb er of less than fullp erio d cycles



to b e precise

The question now b ecomes howdowe initialize separate cycles This problem is ad

dressed by Mascagni et al Mascagni et al where they describ e a canonical form

for initializing Fib onacci generators This canonical form is determined by and k butis

indep endentofM To understand the use of this canonical form consider a second view of

the LF G register shown in Figure The Lshap ed region along the left column

and the b ottom row is xed with all zeros except for a one in the least signicant bit of word

the word asso ciated with X The remaining bits those in the bit rectangular

n

region lled with zs are free to b e chosen in anycombination Eachcombination of bits in

the area will generate a distinct cycle of pseudorandom numb ers In other words

every p ossible bit pattern that can b e put in the canonical form will o ccur in one and only

one full p erio d cycle

In general the canonical form for initializing Fib onacci generators requires that word

b e set to all zero bits and that the least signicantbitsofallwords in the register to

b e set to zero with the exception of one or twocharacteristic bits that dep end on and k

As shown in Figure the canonical form for the LF G generator requires the least

signicant bit of word to b e set to one with all other least signicant bits in the register

set to zero Table lists for several choices of LF G k M the characteristic word or

words for which the least signicant bit should b e set to one in order to b e in canonical

form

Twocaveats with resp ect to the use of this canonical form should b e mentioned at this

p oint Supp ose a user were to construct and initialize a numb er of generators using an initial

ization scheme that simply numb ered the initial states as one would numb er the integers in a

normal binary representation This metho d certainly would not violate the conditions under

which the canonical form pro duces distinct cycles and indeed the cycles of random num

b ers generated from each initial state would b e dierent But the rst few random numbers

from these cycles may b e less than randomlo oking when compared b oth with corresp onding

Addition mod 16

m.s.b. 0 z zzz zzzzz

0 z zzz zzzzz

0 z zzz zzzzz

l.s.b. 000 0 0 0 0 000

X(n-10) X(n-9) X(n-8) X(n-7) X(n-6) X(n-5) X(n-4) X(n-3) X(n-2) X(n-1)

Figure Canonical Form for LFG

output from other generators and with successivevalues from the same generator As an

example consider the LF G generator where we canonically initialize one register

with all zeros in the zed region of Figure call this generator A and a second generator

with a single one in the lower right corner of the zed region call this generator B Table

lists the rst pseudorandom numb ers pro duced by b oth of these generators b oth as

integers in the range and as oating p ointnumb ers in the range Note that

the A sequence consists of lownumb ers less than for iterations and is actually either

zero or one for the rst iterations B is somewhat b etter but still not very satisfying

In addition A and B app ear at least until ab out iterations to b e correlated This ap

p earance of nonrandom b ehavior on the part of these two sample sequences do es not mean

that Fib onacci sequences or this metho d of initializing them do es not pro duce high quality

random numb ers Flat sp ots are to b e exp ected in any go o d pseudorandom numb er se

quence of sucient length and indeed such a sequence without them would b e susp ect with

regard to its randomness The problem with initializing A and B in the example is that the

at sp ots were lined up with each other and were placed at the very b eginning Neither of

these results would b e a problem if our application required the use of thousands of numbers

Remarks

Table Canonical Form Sp ecications for Selected Fib onacci Generators

word nos

k for lsb

from b oth the A and the B sequences and if the numb ers generated at the b eginning were

no more imp ortant than those generated later After a short time the two sequences would

lo ok completely uncorrelated from each other and would app ear internally random as well

However if the numb er of required pseudorandom numb ers is small the outcome of the

numerical exp eriment might b e unexp ectedly skewed due to the initial conditions

The problem just demonstrated is not dicult to x We simply need a dierentway

of numb ering the patterns in the area of the free bits in the canonical form one that

randomizes these patterns to some extent An LCG can b e used to initialize the free bits

if one is careful to use it in suchaway that a unique bit pattern results for each distinct

cycle number number Pryor et al Pryor et al describ e the use of the LCG of Park

and Miller Park and Miller as a go o d metho d for initializing their family of Fib onacci

generators In that family M is equal to so that the free bit rectangle is bits high



whereas the Park and Miller generator uses a mo dulus of the prime number Thus

th

the successivevalues in the high bits of the n initial state for word through word

are simply the LCG successors of n This gives a simple way to initialize



distinct cycles that start out with a satisfactory lo ok and feel As an aside this

particular LCG passes the bitwise randomness tests describ ed by Altman Altman

for use in initializing Fib onacci generators

The second caveat related to the use of this canonical form is also highlighted bythe

previous example The astute reader mayhave noticed that with this metho d of initializing

the Fib onacci register for every advance the least signicant bit is the same for all gener

ators Unfortunately this is the tradeo vs eciency that was made in order to guarantee

uniqueness of the cycles the least signicant bit is simply nonrandom relative to the other

Table The First Random Numb ers from LFGs A and B

X R X R

n n n n

n Seq A Seq B Seq A Seq B n Seq A Seq B Seq A Seq B

Remarks

X(9) X(15)

X(8) X(14)

X(7) X(13) vector length (k-1) X(6) X(12)

X(5) X(11)

X(4) X(10)

X(3) X(9)

X(2) X(8)

X(1) X(7)

X(0) X(6)

Figure Vectorized Stepping of LFGM

cycles For all other bit elds including the nexttoleast signicant there is no such defect

Several p ossibilities arise for remedying this situation including the use of a separate in

dep endent generator for only the least signicantbitHowever in the interest of simplicity

and sp eed Pryor et al havechosen to shift o the least signicant bit of the generated



random numb er so that the numb ers returned by the generator are in the range



rather than The register is initialized and maintained as a bit generator so

that no loss of p erio d length or uniqueness of cycle is incurred And for bit machines the

returned results still include all p ositiveintegers

As mentioned in the b eginning of this section there is at least limited p otential for vec

torization of a single lagged Fib onacci generator Figure is an illustration of vectorization

applied to our LF G M generator As the gure shows the vector algorithm advances

the register ahead byk steps so that the vector length of most of the op erations is

k Note that there is a vector copy op eration of length k Care should

b e taken that no item of data is destroyed b efore it is needed The easiest way to prevent

unintentionally writing over needed data is to keep two copies of the Fib onacci register and

for eachvector advance use the old copy to construct the new one None of the data

in the old copy will b e destroyed until the next vector advance when it b ecomes the new

copyIfvectorization of the Fib onacci generator is imp ortant and it could b e if random

numb er generation consumes a large fraction of the execution time then clearly a long

vector length is b etter than a short one Pro cessing with a vector length of as our ex

ample has would not yield much improvementover the scalar metho d For vectorization

to provide meaningful improvementover scalar pro cessing the vector op erations should b e

long enough to make go o d use of the machine hardware For example on Cray machines

where the vector registers are words long on the new mo dels this usually means

vector lengths of tens of elements For these machines the generators LF G Mand

LF G Mwould be good choices with resp ectivevector lengths of and

In Figure we list a sample Fortran co de for initializing and generating random numbers

from the generator LF G Note that the register is maintained as a set of bit

numb ers but that the numb er returned to the user has only bits The initialization of the

register is accomplished using the Park and Miller LGC describ ed in Park and Miller

The seed iseed supplied by the user maybeanyinteger greater than or equal to zero



and less than or equal to The register is initialized in canonical form

so eachvalue of iseed results in a distinct cycle of random numb ers Since the function

irnd was written to work on bit machines as well as bit machines the mask

op erations were included to add clarity to the co de In many situations the bit mask

op eration could b e eliminated since the hardware would simply ignore anyoverow The

bit mask could also b e eliminated on any systems that zeroll on right shift op erations If

the system p erforms a sign extension typ e of ll then the bit mask would b e required

Exercise to explore LFGs visually

Use the co de ranlff to generate random numb ers Mo dify the co de to print out pairs of

random numb ers on and display them as p oints as R vs R for p oints

n n

random numb ers

a Using n varies from to and k select a subtractive generator Use

an initial seed of to generate the initial state Compare the display to those of Figures

af in Section whichwere generated using LCG generators

b Select dierentvalues for k butkeep xed at Use the parameters as in a

ab ove Generate plots as ab ove in a What do you observe ab out the uniformityofthe

random numb ers and their correlation

Remarks

call initiseed

krand irnd

krand irnd

krand irnd

c

c

subroutine initiseed

implicit integer az

parametera r q m zfffffff

commonc ifibreglptrkptr

c

do i

hi iseedq

lo modiseedq

iseed alo rhi

ifiseedlt iseed iseed m

ifibregi ishftiseed

enddo

ifibreg ifibreg

lptr

kptr

return

end

c

c

integer function irnd

parametermask zffffffff mask zfffffff

commonc ifibreglptrkptr

c

itemp ifibreglptr ifibregkptr

ifibreglptr ianditempmask

lptr lptr

kptr kptr

iflptrle lptr

ifkptrle kptr

irnd iandishftitempmask

return

end

Figure FORTRAN implementation of LF G

Exercise the fundamental concepts of LFGs

Consider the Fib onacci generator LF G

a Draw a shift register diagram similar to Figure to represent the action of this

generator

b How long is the p erio d P of this generator

c How many distinct cycles do es this generator have

d Using the information for this generator in Table initialize in canonical form every

cycle for this generator and generate each full cycle

e Supp ose the generator LF G were initialized with the values X X

 

and X If the generator were advanced for a full cycle which of the canonical cycles



generated in part d would b e the same as this new one How manyadvances of the

generator did you need to determine this

f Would parts a through e b e fun to do if the generator changed only slightly from

LF G to LF G What is the value of P for LF G Howmany distinct

cycles do es it have

Exercise various LFGs as they inuence the number and

length of cycles

Using a computer and your favorite programming language go through steps a through e

as in Exercise ab ove for the generator LF G What ab out part f ie changing

LF G to LF G What would that change do to the numb er of cycles and to

P

Summary and Recommendations

Wehave illustrated through example applications the implementation of various random

numb er generators Wehave concerned ourselves much more with issues of p ortabilityand

quality than with eciency as our observations have b een that generating random numbers

is not very consumptive of CPU resources except in a narrow range of circumstances Our

observations have led us to conclude that now due to more p owerful computers more

sophisticated physical mo dels are employed This results in co des whichdomorework p er

random trial than in the past

Although this is not a formal treatment wehave b een able to infer that there exist go o d

random numb er generators for bit machines The p ortable generator recommended by

Park and Miller Park and Miller app ears to b e particularly suitable for ordinary



problems Atvery large scale this random numb er generators p erio d of over

billion may b e to o short

Using lagged Fib onacci generators shows great promise and is now undergoing thorough

testing particularly in terms of correlations of sequences It app ears particularly suitable

for generating many streams of indep endentrandomnumb ers in parallel The interested

Remarks

reader should stay tuned for further developments Moreover we b elieve these generators

are now suciently mature that we recommend them for inclusion in existing applications

The quality of the random sequences are at least as go o d as those generated using LCGs

with the only drawback b eing the requirement for greater storage In cases requiring many

parallel generators this can b e a factor

Finallywewax philosophical We urge the sup ercomputist to approach the generation of

random numb ers with circumsp ection particularly when solving very largescale problems

All random numb er generators should b e tested thoroughly for their quality b efore b eing

used up on other than academic problems Further at least twolevels of quality in random

numb er generators should b e implemented to assess whether the answers are indep endentof

the random numb ers This is a matter requiring judgment and is aided by exp erience

However the situation is not as bleak as it may seem from the ab ove discussion For very

large scale problems lacking a regular structure eg complex geometries andor material

prop erties it is very unlikely that correlations b etween random numb ers generated on sep

arate pro cessors will b e signicant due to the fact that the physical problem exhibits a great

many p ossibilities for random events Further if the parameters of the generator are chosen

correctly there are a great many p ossibilities for random events when using random real

numb ers However the truly cautious researcher should not rely on the conuence of these

factors to discourage assessing the results in the context of quality of the random events

derived from the random numb er generator

Further it is wise to mention that the ab ove observations are sp ecic to using random real

numb ers and not random integers In particular there may exist a resonance b etween the

physical problem and random integers such as the successive o ddeven pairs obtained when

using a mo dulus of a p ower of two in a linear congruential generator As the complexity

of the problem decreases the p otential for problems with the random numb er generator

increases

We note that FORTRAN will obviate many of the imp ediments to a p ortable imple

mentation of random numb er generators as it allows arbitrary precision in numb ers

which can b e made large enough to eliminate overow and problems with a sign bit and

it has emb edded in the language functions to return the date and time whichnow are sys

tem calls and vary from system to system Moreover a random numb er generator intrinsic

function exists as part of the language and it may b e quite go o d although it will haveto

b e tested thoroughly by the Monte Carlo community b efore gaining universal acceptance

References

N S Altman Bitwise Behavior of Random Numb er Generators SIAM J Sci Stat

Comput Septemb er pps G M Amdahl Validity of the single pro

cessor approachtoachieving largescale computing capabilities Proceedings of the American

Federation of Information Processing Societies Washington DC pps

S L Anderson Random Numb er Generators on Vector Sup ercomputers and Other

Advanced Architectures SIAM Review pps

E F Beckenback ed Modern Mathematics for the Engineer McGraw Hill New York

NY

K Binder Applications of the in Statistical Physics Springer

Verlag Berlin

J Briesmeister ed MCNP a general Monte Carlo co de for neutron and photon trans

p ort LAM Rev Los Alamos National Lab oratory rep ort

T B Brown Vectorized Monte Carlo PhD Dissertation Department of Nuclear Engi

neering University of Michigan

C E Burghart and P N Stevens A general metho d of imp ortance sampling the angle

of scattering in Monte Carlo calculations Nuclear Science and Engineering

P Burns M Christon R Schweitzer H Wasserman M Simmons O Lub eckandD

Pryor Vectorization of Monte Carlo particle transp ort an architectural study using the

LANL b enchmark GAMTEB Proceedings Supercomputing Reno NV Nov

P J Burns and D V Pryor Vector and parallel considerations for the Rayleigh problem

in molecular gas dynamics Proceedings th International Conference on Finite Element

Methods in Flow ProblemsHuntsville AL April

P J Burns and D V Pryor Vector and parallel Monte Carlo radiative heat transfer

Numerical Heat Transfer Part B Fundamentals

R D Chandler J N Panaia R B Stevens and G E Zinmeister The solution of

steadystate convection problems by the xed random walk metho d Journal of Heat Trans

fer

Cray Research Inc UNICOS Libraries Macros and Op defs Reference Manual Publica

tion SR Cray Research Inc Mendota Heights MN

D Cro ckett J D Maltby and P J Burns MONTV users manual Internal Publi

cation DepartmentofMechanical Engineering Colorado State University

J H Curtis Sampling Metho ds Applied to Dierential and Dierence Equations

Proceedings IBM Seminar on Scientic ComputationNov IBM Corp New York

L Dagum Implementation of a hyp ersonic rareed ow particle simulation on the

connection machine Proceedings Supercomputing ACM Press Baltimore MD

Nov

R Eckhard Stan Ulam John von Neumann and the Monte Carlo metho d Los Alamos

Science

Remarks

L W Ehrlich Monte Carlo Solutions of Boundary Value problems Involving the Dif

  

u v w

ference Analogue of Journal of the Association of Computing Machinery

  

x y z

A F Emery and W W Carson A mo dication to the Monte Carlo metho d the

exo dus metho d Journal of Heat Transfer

PFrederickson R Hiromoto T L Jordan B Smith T Warno ck Pseudorandom

trees in Monte Carlo Paral lel Computing Decemb er

S W Golomb Shift Register Sequences Revised Ed Aegean Park Press

A Ha jiSheikh Monte Carlo Metho ds Ch in Handbook of Numerical Heat Transfer

A Ha jiSheikh and E M Sparrow The Floating Random Walk and its Application to

Monte Carlo Solutions of Heat Equations Journal of Heat Transfer

J M Hammersly and D C Handscomb Monte Carlo MethodsMethuen London

D B Heifetz Vectorizing and macrotasking Monte Carlo neutral particle algorithms

Princeton Plasma Physics Lab oratory Rep ort PPPL April

J R Howell Application of Monte Carlo to Heat Transfer Problems Advances in Heat

Transfer

C C Hurd ed Proceedings of the Seminar on Scientic ComputationInternational

Business Machines Corp oration New York NY Nov

H Kahn and A W Marshall Metho ds of reducing sample size in Monte Carlo compu

tations Journal of Operations Research Society of America

M H Kalos Monte Carlo metho ds and the computers of the future Ultracomputer

Note April

M A Kalos and P A Whitlo ck Monte Carlo Methods Volume I Basics Wiley Inter

science New York

J P C Kleinjnen Statistical Techniques in Simulation Part Marcel Dekker New

York NY

D E Knuth Seminumerical Algorithms nd Ed Vol of The Art of Computer

Programming AddisonWesley Reading PA

J M Lanore Weighting and biasing of a Monte Carlo calculation for very deep p ene

tration of radiation Nuclear Science Engineering

M E Larsen and J R Howell Leastsquares smo othing of directexchange areas in

zonal analysis Journal of Heat Transfer

J D Maltby Threedimensional Simulation of Radiative Heat Transfer by the Monte

Carlo Method Masters Thesis DepartmentofMechanical Engineering Colorado State Uni

versity

J D Maltby Analysis of Electron Heat Transfer via Monte Carlo Simulation PhD

Dissertation DepartmentofMechanical Engineering Colorado State University

J D MaltbyandP J Burns MONTD and MONTD users manual Internal Pub

lication DepartmentofMechanical Engineering Colorado State University

J D Maltby and P J Burns MONTE users manual Internal Publication Depart

ment of Mechanical Engineering Colorado State University

G Marsaglia A Current View of Random Numb er Generators Computer Science and

Statistics The Interface Elsevier Science Publishers B V North Holland L Billard ed

W R Martin PFNowak and J A Rathkopf Monte Carlo photon tracing on a vector

sup ercomputer IBM Journal of Research and Development

M Mascagni S Cuccaro D Pryor and M Robinson A fast high quality repro ducible

parallel laggedFib onacci pseudorandom numb er generator SRCTR Sup ercom

puting ResearchCenter Science Drive Bowie MD

J D McDonald and D Bagano Vectorization of a particle simulation metho d for

hyp ersonic rareed ow AIAA AIAA Thermophysics Plasmadynamics and Lasers

Conference San Antonio June

N Metrop olis The b eginning of the Monte Carlo metho d Los Alamos Science

N Metrop olis Monte Carlo in the b eginning and some great exp ectations Monte

Carlo Metho ds and Applications in Neutronics Photonics and Statistical Physics Cadarache

Castle France

M F Mo dest Monte Carlo metho d in analysis of threedimensional radiative exchange

factors for nongray nondiuse surfaces Numerical Heat Transfer

M E Mueller Some continuous Monte Carlo metho ds for the Dirichlet problem

Annals of Mathematical Statistics

S K Park and K W Miller Random Numb er Generators Go o d Ones are Hard to

Find Transactions of the ACMNov

L C Polgar and J R Howell Directional thermalradiative prop erties of conical cavi

ties NASA TN D

W H Press et al Numerical Recipes FORTRAN pps

D V Pryor and P J Burns A parallel Monte Carlo mo del for radiative heat transfer

Presented at the SIAM Meeting Boston MA July

Remarks

D V Pryor and P J Burns Vectorized molecular aero dynamics simulation of the

Rayleigh problem Proceedings Supercomputing Orlando FL Nov

D Pryor S Cuccaro M Mascagni and M Robinson Implementation and usage of

a p ortable and repro ducible parallel pseudorandom numb er generator SRCTR

Sup ercomputing Research Center Science Drive Bowie MD

Lord Rayleigh On James Bernoullis Theorem in Probabilities Philosophical Maga

zine pp

S M Ross A First Course in Probability nd Ed Macmillan New York NY

R Y Rub enstein Simulation and the Monte Carlo Method WileyNewYork NY

Y A Schreider Methods of Statistical Testing Elsevier New York NY

Sequent Computer Systems Parallel ray tracing study TNrvp Rev

N Shamsunder E M Sparrow and R P Heinisch Monte Carlo radiative solutions

eect of energy partitioning and number of rays International Journal of Heat and Mass

Transfer

S Ulam and N Metrop olis The Monte Carlo metho d Journal of American Statistical

Association

S Ulam R D Richtmeyer and J von Neumann Statistical metho ds in neutron diu

sion LAMS Los Alamos National Lab oratory

Y Q Zhong and M H Kalos Monte Carlo transp ort calculations on an Ultracom

puter Ultracomputer Note March

G E Zinsmeister and J A Sawyer A metho d for improving the eciency of Monte

Carlo calculation for Dirichlet problems Journal of Heat Transfer