|
HG-MD
1
|
This is a class that generates random numbers i.e. More...
#include <RNG.h>
Public Member Functions | |
| RNG () | |
| void | set_RandomSeed (Mdouble new_seed) |
| This is the seed for the random number generator. | |
| Mdouble | get_RN (Mdouble min, Mdouble max) |
| This is a random generating routine can be used for initial positions. | |
| Mdouble | test () |
| This function tests the quality of random numbers, based on the chi-squared test. | |
| void | set_LCGParms (int new_a, int new_c, int new_m) |
| This functions set the parameters for the LCG random number generator. It goes multiplier, addition, mod. | |
| void | randomise () |
| sets the random variables such that they differ for each run | |
| void | set_LFGParms (int new_p, int new_q) |
| This function sets the parametes for the LFG random number generator. | |
| void | set_RNtypeLCG () |
| Make the random number generator based on LCG. | |
| void | set_RNtypeLFG () |
| Make the random number generator based on LFG. | |
Private Member Functions | |
| Mdouble | get_LCG (Mdouble min, Mdouble max) |
| This is a basic Linear Congruential Generator Random. | |
| Mdouble | get_LFG (Mdouble min, Mdouble max) |
| This is a Laggend Fibonacci Generator. | |
| void | seed_LFG () |
| This seed the LFG. | |
Private Attributes | |
| unsigned long int | random_seed_LCG |
| This is the initiall seed of the RNG. | |
| vector< Mdouble > | random_seed_LFG |
| This is the seeds required for the LFG. | |
| long int | a |
| This are the two parameters that control the LCG random generated. | |
| long int | c |
| long int | m |
| long int | p |
| This are the parameters that control the LFG random generator. | |
| long int | q |
| int | type |
| This is the type of random number generator. | |
This is a class that generates random numbers i.e.
named the Random Number Generator (RNG). This is a stand-along class; but is encapsulated (used) by the MD class. To make it architecture safe the both LCG and function is hard codes i.e. does not use the internal C++ one.
| RNG::RNG | ( | ) | [inline] |
References a, c, m, p, q, random_seed_LCG, random_seed_LFG, seed_LFG(), and type.
{random_seed_LCG=0; a=1103515245; c=12345;m=1024*1024*1024;type=1;p=607;q=273;random_seed_LFG.resize(p); seed_LFG();}
| Mdouble RNG::get_LCG | ( | Mdouble | min, |
| Mdouble | max | ||
| ) | [private] |
This is a basic Linear Congruential Generator Random.
This is a basic Linear Congruential Generator Random Is described by three parameters, the multiplication a, the addition c and the mod m.
References a, c, m, and random_seed_LCG.
Referenced by get_RN(), and seed_LFG().
{
//Update the random seed
random_seed_LCG = (a*random_seed_LCG + c) % m;
//Generate a random number in the required range
Mdouble random_num;
Mdouble range=max-min;
random_num = min+range*random_seed_LCG/(static_cast<Mdouble>(m) + 1.0);
return random_num;
}
| Mdouble RNG::get_LFG | ( | Mdouble | min, |
| Mdouble | max | ||
| ) | [private] |
This is a Laggend Fibonacci Generator.
This is a basic Linear Fibonacci Generator Random Is described by three parameters, the multiplication a, the addition c and the mod m.
References p, q, and random_seed_LFG.
Referenced by get_RN().
{
Mdouble new_seed=fmod(random_seed_LFG[0]+random_seed_LFG[p-q],(Mdouble) 1.0);
//Update the random seed
for (int i=0;i<p-1;i++)
{
random_seed_LFG[i]=random_seed_LFG[i+1];
}
random_seed_LFG[p-1]=new_seed;
//Generate a random number in the required range
Mdouble random_num;
Mdouble range=max-min;
random_num = min+range*new_seed;
return random_num;
}
| Mdouble RNG::get_RN | ( | Mdouble | min, |
| Mdouble | max | ||
| ) |
This is a random generating routine can be used for initial positions.
This is a random number generator and returns a Mdouble within the range specified See also MD::set_random_seed()
References get_LCG(), get_LFG(), and type.
Referenced by Chute::create_bottom(), Chute::create_inflow_particle(), ChuteWithHopper::create_inflow_particle(), ChuteBottom::setup_particles_initial_conditions(), and test().
| void RNG::randomise | ( | ) | [inline] |
sets the random variables such that they differ for each run
References set_RandomSeed().
Referenced by MD::readNextArgument().
{set_RandomSeed(time(NULL));}
| void RNG::seed_LFG | ( | ) | [private] |
This seed the LFG.
References get_LCG(), p, and random_seed_LFG.
Referenced by RNG(), set_LFGParms(), and set_RandomSeed().
{
for (int i=0;i<p;i++)
{
random_seed_LFG[i]=get_LCG(0,1.0);
}
}
| void RNG::set_LCGParms | ( | int | new_a, |
| int | new_c, | ||
| int | new_m | ||
| ) | [inline] |
| void RNG::set_LFGParms | ( | int | new_p, |
| int | new_q | ||
| ) |
This function sets the parametes for the LFG random number generator.
References p, q, random_seed_LFG, and seed_LFG().
| void RNG::set_RandomSeed | ( | Mdouble | new_seed | ) | [inline] |
This is the seed for the random number generator.
References random_seed_LCG, and seed_LFG().
Referenced by MD::constructor(), and randomise().
{random_seed_LCG=new_seed; seed_LFG();}
| void RNG::set_RNtypeLCG | ( | ) | [inline] |
| void RNG::set_RNtypeLFG | ( | ) | [inline] |
This function tests the quality of random numbers, based on the chi-squared test.
It reports a probabity that the random number being generated are coming from a uniform distributed. If this number is less than 0.95, it is strongly advised that you change the parameters being used
References mathsFunc::chi_squared_prob(), and get_RN().
{
//This are the fixed parameters that define the test
static int num_of_tests=100000;
static Mdouble max_num=100.0;
static int num_of_bins=10;
//This is the generated random_number
Mdouble rn;
//This is the bin the random number will lie in
int bin=0;
//This is a vector of bins
vector<int> count;
count.resize(num_of_bins);
//Initialisation of the bins
for (int i=0;i<num_of_bins;i++)
{
count[bin]=0;
}
//Loop over a number of tests
for (int i=0;i<num_of_tests;i++)
{
rn=get_RN(0.0,max_num);
bin=floor(rn*num_of_bins/max_num);
//Add one to the bin count
count[bin]++;
}
//Final post-process the result and report on the random number
Mdouble chi_cum=0.0;
Mdouble expected=num_of_tests/num_of_bins;
for (int i=0;i<num_of_bins;i++)
{
chi_cum=chi_cum+(count[i]-expected)*(count[i]-expected)/expected;
cout << i << " : " << count[i] << " : " <<(count[i]-expected)*(count[i]-expected)/expected << endl;
}
//end for loop over comuputing the chi-squared value.
cout << chi_cum << endl;
return mathsFunc::chi_squared_prob(chi_cum,num_of_bins);
}
long int RNG::a [private] |
This are the two parameters that control the LCG random generated.
Referenced by get_LCG(), RNG(), and set_LCGParms().
long int RNG::c [private] |
Referenced by get_LCG(), RNG(), and set_LCGParms().
long int RNG::m [private] |
Referenced by get_LCG(), RNG(), and set_LCGParms().
long int RNG::p [private] |
This are the parameters that control the LFG random generator.
Referenced by get_LFG(), RNG(), seed_LFG(), and set_LFGParms().
long int RNG::q [private] |
Referenced by get_LFG(), RNG(), and set_LFGParms().
unsigned long int RNG::random_seed_LCG [private] |
This is the initiall seed of the RNG.
Referenced by get_LCG(), RNG(), and set_RandomSeed().
vector<Mdouble> RNG::random_seed_LFG [private] |
This is the seeds required for the LFG.
Referenced by get_LFG(), RNG(), seed_LFG(), and set_LFGParms().
int RNG::type [private] |
This is the type of random number generator.
Referenced by get_RN(), RNG(), set_RNtypeLCG(), and set_RNtypeLFG().
1.7.6.1