Sie sind auf Seite 1von 19

Monte Carlo Methods

1953, Nicolaus Metropolis


Monte Carlo method refers to any method that makes use of random
numbers
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
sequence
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
algorithm.
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.

M I X
n n
/
An early example (John Von
Neumann,1946)
To generate 10 digits of integer
Start with one of 10 digits integers
Square it and take middle 10 digits from answer
Example: 5772156649
2
= 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:
6100
2
= 37210000
2100
2
= 04410000
4100
2
= 16810000
5100
2
= 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).
Uniformity
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.
Efficiency
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
Integration
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
decay
Sample : N nuclei which decay at a rate
1
.
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 &

+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

and

, each vs time for all t.



Problem
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
Class
Member functions
Member variables
#include<iostream>
#include<cmath>
#include<fstream>
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;
public:
float Rnd()
{
a=l*99;
b=a/10000;
num=int(b);
ran=b-num;
l=ran*10000;
return ran;
}
void getdata();
void cal();
};
void RDCY::getdata()
{
cout<<"enter the seed value"<<endl;
cin>>l;
cout<<"enter the no. of parent nuclei"<<endl;
cin>>p;
cout<<"enter the no. of daughter nuclei"<<endl;
cin>>d;
gd=0; //initial number of granddaughter nuclei
cout<<"enter the decay constant for parent nuclei"<<endl;
cin>>dcp;
cout<<"enter the decay constant for daughter nuclei"<<endl;
cin>>dcd;
cout<<"enter the time step"<<endl;
cin>>h;
t=0;
cout<<"enter the no. of time steps"<<endl;
cin>>n;
}

Input Data through
console
void RDCY::cal()
{
for(t=0;t<n;t=t+h)
{
output<<t<<"\t"<<p<<"\t"<<d<<"\t"<<gd<<endl;
for(int i=0;i<p;i++)
{
float x=Rnd();
if(x<=dcp)
{
p=p-1;
d=d+1;
}
}
for(int i=0;i<d;i++)
{
float x=Rnd();
if(x<=dcd)
{
d=d-1;
gd=gd+1;
}
}
}
}

Calculation of
Radioactive Decay
int main()
{
class RDCY R;
R.getdata();
R.cal();
return 0;
}

Function calling
Output