Random numbers and Monte Carlo simulations 
A D V E R T I S E M E N T
All computer simulations (e.g. rolling the dice, tossing a
coin) make use of a function that gives a random number uniformly between
[0,1), i.e. includes 0, but not 1. In Fortran this function is
ran(seed), where seed
is an integer variable used to generate (“seed”) the sequence of random
numbers. Below is a sample program that will generate 10 random numbers in the
interval [0,1).
Random numbers and Monte Carlo simulations 
program random
integer seed, n
seed=7654321
c seed should be set to a large odd integer according to
the Fortran manual
n = 10
do n=1,10
r=ran(seed)
write(6,*) n, r
c could use a * instead of 6 in the write statement
enddo
stop
end

The seed is used to generate
the random numbers. If two programs (or the same program run twice) use the same
seed, then they will get the same sequence of random numbers. Try running the
above program twice! If you want a different sequence of random numbers every
time you run your program then you must change your seed. One way to have your
program change the seed for you is to use a function that gives a number that
depends on the time of day. For example using the function secnds(x)
gives the number of seconds
(minus x) since midnight. So, as long as you don’t rerun your program too
quickly, or run it exactly the same time on two different days, this Fortran
program will give a different set of random numbers every time it is run.
program random
integer seed,seed1, n
real x
seed1=7654321
x=0.0
c seed should be set to a large odd integer according to
the manual
c secnds(x) gives number of secondsx elapsed since
midnight
c the 2*int(secnds(x)) is always even (int=gives integer)
so seed is always odd
seed=seed1+2*int(secnds(x))
n = 10
do n=1,10
r=ran(seed)
write(6,*) n, r
c could use a * instead of 6 in the write statement
enddo
stop
end

The random number generator
function only gives numbers in the interval [0,1). Sometimes we want random
numbers in a different interval, e.g. [1,1). A simple transformation can be
used to change intervals. For example, if we want a random number (x) in the
interval [a,b) we can do so using:
x=(ba)*ran(seed)a
Thus for the interval [1,1)
we get: x=2*ran(seed)1.
In fact, we can take our set of
numbers from the ran(seed) function which have a uniform probability
distribution in the interval [0,1) and turn them into a set of numbers that look
like they come from just about any probability distribution with any interval
that one can imagine!
A few examples:
dice=int(1+6*ran(seed))
This generates the roll of a 6 sided die.
g=sqrt(2*log(ran(seed)))*cos(2*pi*ran(seed))
This generates a random
number from a gaussian distribution with mean=0 and variance=1. We assume that
pi is already initialized to 3.14159 in the program.
t= a*log(ran(seed)) This
generates a random number from an exponential distribution with lifetime =a.
Being able to transform the
random numbers obtained from ran(seed) into any probability distribution
function we want is extremely useful and forms the basis of all computer
simulations.
This type of simulation often
goes by the name “Monte Carlo”. Why Monte Carlo? In the precomputer era a
popular way to obtain a set of random numbers was to use a roulette wheel, just
the type found in the Mediterranean city of Monte Carlo, famous for its gambling
casino. This all happened in the late 1940’s. If this technique had become
popular in the late 1950’s we’d probably be calling it Las Vegas!
