About random number generator
KVS has the two types of the random number generator:
- uniform random number: MersenneTwister
- normal random number: BoxMuller
In this page, we explain the member function of the random class, and then, Let’s calculate the circumference ratio by using the Monte Carlo method. Moreover, we obtain the average and variance.
Member functions
Constructor
There are two types of constructor.
If you set the seed, the random number sequence is fixed.
// Default constructor
kvs::MersenneTwister uniRand; // uniform random
kvs::BoxMuller normRand; // normal random
// Set to a seed for the random number generator
kvs::MersenneTwister uniRand2( 261422 ); // uniform random
kvs::BoxMuller normRand2( 261422 ); // normal random
Generation of random number
perator (): generation of random number
KVS sets as default random number:
- Uniform random: uniformly distributed in [0, 1]
- Normal random: standard normal distribution as N(0,1)
std::cout << "[0, 1] : uniform random " << uniRand() << std::endl;
std::cout << "N(0, 1) : normal random " << normRand() << std::endl;
std::cout << "[0, 1] : uniform random " << uniRand2() << std::endl;
std::cout << "N(0, 1) : normal random " << normRand2() << std::endl;
Change the distribution
Uniformly distributed in [0, n] :
rand( const double n )
ormal distribution as N(mu, sigma2):
rand( const double mu, const double sigma2 )
where mu; average,sigma2 :variance.
// Change the distribution
std::cout << "[0, 100] : uniform random " << uniRand.rand( 100 ) << std::endl;
std::cout << "N(2, 10) : normal random " << normRand.rand(2, 10) << std::endl;
Example: Calculate π by using the Monte Carlo method
If the circle of radius r is inscribed inside the square with side length 2r, then the area of the circle will be π*r^2 and the area of the square will be (2r)^2. The ratio of the area of the circle to the area of the square will be π/4.
In this example, r=1, and, the random number is generated in[0, 1]×[0, 1], and then, you count the number of pints inside the circle x2 + y2 < 1 .
1. Make a working directory
First, please create a working directory where you will run the program (e.g., CreateRandom).
# mkdir CreateRandom
# cd CreateRandom
2. Programming
In order to generate the uniform random, you create an instance of kvs::MersenneTwister.
In this example, the number of trials is fixed as 1000.
For example:
int main( void )
{
kvs::MersenneTwister uniRand; // uniform random
// Number of trials
const int trialNum = 1000;
return ( 0 );
}
3. Initializing
The variable for counting the number of point inside the circle set to zero. In addition, the variables for calculating the average and the variance set to zero.
// For counting the number of point
int num = 0;
// Average and variance
double uniAve = 0.0, uniDp = 0.0;
4. Counting
you count the number of pints inside the circle x2 + y2 < 1 .
for( int i=0; i<trialNum; i++ ) {
// Generate random number
double x = uniRand();
double y = uniRand();
// Count the number of point inside the circle
if( ( x*x + y*y ) < 1.0 )
num++;
// Calculate the sum and square sum
uniAve += x;
uniDp += ( x * x );
}
5. Calculate π
In order to obtain pi, you calculate the ratio of the number the number of point inside the circle to all number of generated points.
// calculate pi
double pi = (double)num/(double)trialNum * 4.0;
std::cout << num << " " << trialNum << std::endl;
std::cout << "pi: " << pi << std::endl;
// average and variance
uniAve /= (double)trialNum;
uniDp = uniDp/(double)trialNum - uniAve*uniAve;
std::cout << "-- uniform random number-- " << std::endl;;
std::cout << "Average : " << uniAve << " Variance : " << uniDp << std::endl;
6. Sample program
Sample program: CreateRandom.tgz
7. Compile and execute
Let's try to compile and execute above program.
When compiling a KVS program, it is easy to use the automatic Makefile-generation tool kvsmake supplied by KVS.
# kvsmake -G <-- Generate Makefile
# kvsmake <-- Compile
And then,
# ./CreateRandom
===== execution result =====
[0, 1] : Uniform random 0.878668
N(0, 1) : normal random 0.0767265
[0, 1] : Uniform random 0.606438
N(0, 1) : normal random -0.447015
[0, 100] : Uniform random number 76.839
N(2, 10) : normal random 12.1152
pi : 3.136
-- Uniform random Number --
Average : 0.503782 Variance : 0.0857954
Last Updated at Nov. 1, 2015