Sie sind auf Seite 1von 19

Monte Carlo Methods

1953, Nicolaus Metropolis

Monte Carlo method refers to any method that makes use of random
Simulation of natural phenomena
Simulation of experimental apparatus
Numerical analysis

Random Numbers
What is random number ? Is 3 ?
There is no such thing as single random number
Random number
A set of numbers that have nothing to do with the other numbers in the
In a uniform distribution of random numbers in the range [0,1] , every
number has the same chance of turning up.
0.00001 is just as likely as 0.5000

Random v. Pseudo-random
Random numbers have no defined sequence or formulation. Thus,
for any n random numbers, each appears with equal probability.
If we restrict ourselves to the set of 32-bit integers, then our numbers
will start to repeat after some very large n. The numbers thus clump
within this range and around these integers.
Due to this limitation, computer algorithms are restricted to
generating what we call pseudo-random numbers.

How to generate random numbers ?
Use some chaotic system (Balls in a barrel Lotto)
Use a process that is inherently random
Radioactive decay
Thermal noise
Cosmic ray arrival
Tables of a few million random numbers
Hooking up a random machine to a computer.

Pseudo Random number generators
The closest random number generator that can be obtained by computer
Usually a uniform distribution in the range [0,1]
Most pseudo random number generators have two things in common
The use of large prime numbers
The use of modulo arithmetic
Algorithm generates integers between 0 and M, map to zero and one.

n n
An early example (John Von
To generate 10 digits of integer
Start with one of 10 digits integers
Square it and take middle 10 digits from answer
Example: 5772156649
= 33317792380594909291
The sequence appears to be random, but each number is determined from the
previous not random.
Serious problem : Small numbers (0 or 1) are lumped together, it can get itself to
a short loop. For example:
= 37210000
= 04410000
= 16810000
= 65610000

Properties of Pseudorandom Numbers
Uncorrelated Sequences
The sequences of random numbers should be serially uncorrelated
Long Period
The generator should be of long period (ideally, the generator should not repeat;
practically, the repetition should occur only after the generation of a very large set of
random numbers).
The sequence of random numbers should be uniform, and unbiased. That is, equal
fractions of random numbers should fall into equal ``areas'' in space. Eg. if random
numbers on [0,1) are to be generated, it would be poor practice were more than half to
fall into [0, 0.1), presuming the sample size is sufficiently large.
The generator should be efficient. Low overhead for massively parallel computations.

Initializing with Seeds
Most of the algorithms have some state that can be initialized. Many
times this is the last generated number (not thread safe).
You can set this state using the routines initialization methods
Why would you want to do this?

Initializing with Seeds
Most of the algorithms have some state that can be initialized. Many
times this is the last generated number (not thread safe).
You can set this state using the routines initialization methods.
Why would you want to do this?
1. The default state always generates the same sequence of random numbers.
Not really random at all, particularly for a small set of calls. Solution: Call
the seed method with the lower-order bits of the system clock.
2. You need a deterministic process that is repeatable.

Monte-Carlo Techniques
Sample Applications
System simulation
Computer graphics - Rendering.
Physical phenomena - radiation transport
Simulation of Bingo game
Communications - bit error rates
VLSI designs - tolerance analysis

Radioactive Decay
Discrete process with each atom having some finite probability of
Sample : N nuclei which decay at a rate
Decay rate :

Undecayed Nuclei at time t : N(t) =

Probability P that a nucleus undergoes radioactive decay in time

The Monte Carlo Approach
In a given time step, each nucleus is given the opportunity to decay with probability
p. So for a given nucleus, we choose a random number in the range 0 to 1. If the
number is less than the probability p, then it decays. Here decay simply means that
the total number of nuclei is decrease by one. Note that this happens every time
step .
Pseudo code

1. Input initial number of parent nuclei

, and daughter nuclei

2. Input decay constant ,
3. Input the number of time steps ,
4. Input time step ,
5. Loop: t from 0 to ,
6. Loop: i from 0 to

7. Generate a random number R,
8. If R< then

1 &

9. In case daughter nuclei also decay add steps 5 to 8 for

10. Output

and (in case daughter nuclei also decay) to a file,

11. Plot


, each vs time for all t.

Given a sample of 10000 radioactive nuclei each of which decays at rate p per
second, what is the number of parent & daughter nuclei in the sample after an
interval of 5 s, for first 20 s, if =0.02?
Also calculate the number of nuclei, if the daughter nuclei also decay with =0.03.
Source Code
Member functions
Member variables
using namespace std;
ofstream output("out.dat");
class RDCY
int i,p,d,gd,n;
float dcp,dcd,h,t,a,b,num,ran,l;
float Rnd()
return ran;
void getdata();
void cal();
void RDCY::getdata()
cout<<"enter the seed value"<<endl;
cout<<"enter the no. of parent nuclei"<<endl;
cout<<"enter the no. of daughter nuclei"<<endl;
gd=0; //initial number of granddaughter nuclei
cout<<"enter the decay constant for parent nuclei"<<endl;
cout<<"enter the decay constant for daughter nuclei"<<endl;
cout<<"enter the time step"<<endl;
cout<<"enter the no. of time steps"<<endl;

Input Data through
void RDCY::cal()
for(int i=0;i<p;i++)
float x=Rnd();
for(int i=0;i<d;i++)
float x=Rnd();

Calculation of
Radioactive Decay
int main()
class RDCY R;
return 0;

Function calling