Monte Carlo Methods
Monte Carlo Methods
X n I n / M
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: 57721566492 = 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:
• 61002 = 37210000
• 21002 = 04410000
• 41002 = 16810000
• 51002 = 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 rate :
5. Loop: t from 0 to 𝑛,
•
•
• 6. Loop: i from 0 to ,
10. Output , and 𝑁𝑔𝑑(in case daughter nuclei also decay) to a file,
• 9. In case daughter nuclei also decay add steps 5 to 8 for ,
•
• 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 #include<iostream>
Code #include<cmath>
#include<fstream>
using namespace std;
ofstream output("out.dat");
class RDCY
{
Class int i,p,d,gd,n;
Member functions float dcp,dcd,h,t,a,b,num,ran,l;
public:
Member variables 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;
Input Data through cin>>p;
cout<<"enter the no. of daughter nuclei"<<endl;
console 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;
}
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();
Calculation of if(x<=dcp)
{
Radioactive Decay 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;
}
}
}
}
Function calling int main()
{
class RDCY R;
R.getdata();
R.cal();
return 0;
}
Output