#include <randnum.h>
Collaboration diagram for EMAN::Randnum:
Public Member Functions | |
void | set_seed (unsigned long long seed) |
Set the seed for the random number generator. | |
unsigned long long | get_seed () |
Get the current random number seed. | |
long long | get_irand (long long lo, long long hi) const |
This function returns a random integer from lo to hi inclusive. | |
float | get_frand (double lo=0.0, double hi=1.0) const |
This function returns a random float from lo inclusive to hi. | |
float | get_frand_pos (double lo=0.0, double hi=1.0) const |
This function returns a random float from lo to hi. | |
float | get_gauss_rand (float mean, float sigma) const |
Return a Gaussian random number. | |
void | print_generator_type () const |
print out all possible random number generator type in gsl | |
Static Public Member Functions | |
static Randnum * | Instance () |
static Randnum * | Instance (const gsl_rng_type *_t) |
Protected Member Functions | |
Randnum () | |
The default constructor will use the gsl default random number generator gal_rng_mt19937. | |
Randnum (const Randnum &) | |
Randnum (const gsl_rng_type *_t) | |
This constructor is for setting the new random number generator engine other than gsl default gsl_rng_mt19937. | |
~Randnum () | |
Static Private Attributes | |
static const gsl_rng_type * | T = gsl_rng_default |
static gsl_rng * | r = 0 |
static unsigned long long | _seed = random_seed() |
static Randnum * | _instance = 0 |
This class is a singleton class. the default random number generator engine is gsl default: gsl_rng_mt19937, gsl_rng_mt19937 has a period 2^19937. the default seed is from /dev/random or milli second of current time.
Randnum *r = Randnum::Instance(); int i = r->get_irand(lo, hi); *
Randnum *r = Randnum::Instance(); float f = r->get_frand(); *
Randnum *r = Randnum::Instance(); float f = r->get_frand_pos(); *
Randnum *r = Randnum::Instance(); float f = r->get_frand(lo, hi); *
Randnum *r = Randnum::Instance(); float f = r->get_frand_pos(lo, hi); *
Randnum *r = Randnum::Instance(); float f = r->get_gauss_rand(mean, sigma); *
Randnum *r = Randnum::Instance(); r->set_seed(12345); *
Randnum *r = Randnum::Instance(gsl_rng_rand); //gsl version of rand() Randnum *r = Randnum::Instance(gsl_rng_random128_libc5); //gsl version of Linux's random() *
Definition at line 90 of file randnum.h.
Randnum::Randnum | ( | ) | [protected] |
The default constructor will use the gsl default random number generator gal_rng_mt19937.
This is a very good choice which has a period of 2^19937 and not sacrifice performance too much.
Definition at line 129 of file randnum.cpp.
Referenced by Instance().
EMAN::Randnum::Randnum | ( | const Randnum & | ) | [protected] |
Randnum::Randnum | ( | const gsl_rng_type * | _t | ) | [explicit, protected] |
This constructor is for setting the new random number generator engine other than gsl default gsl_rng_mt19937.
For example: gsl_rng_rand is the gsl version of rand() function, which is 100% faster than the rand() function in C.
_t | the random number generator type |
Definition at line 135 of file randnum.cpp.
Randnum::~Randnum | ( | ) | [protected] |
float Randnum::get_frand | ( | double | lo = 0.0 , |
|
double | hi = 1.0 | |||
) | const |
This function returns a random float from lo inclusive to hi.
lo | the low end of the random float | |
hi | the high end of the random float |
Definition at line 162 of file randnum.cpp.
References r.
Referenced by EMAN::Util::get_frand(), EMAN::TestImageNoiseUniformRand::process_inplace(), and EMAN::AddRandomNoiseProcessor::process_inplace().
00163 { 00164 return static_cast<float>(gsl_rng_uniform(r) * (hi -lo) + lo); 00165 }
float Randnum::get_frand_pos | ( | double | lo = 0.0 , |
|
double | hi = 1.0 | |||
) | const |
This function returns a random float from lo to hi.
lo | the low end of the random float | |
hi | the high end of the random float |
Definition at line 167 of file randnum.cpp.
References r.
Referenced by get_gauss_rand().
00168 { 00169 return static_cast<float>(gsl_rng_uniform_pos(r) * (hi -lo) + lo); 00170 }
float Randnum::get_gauss_rand | ( | float | mean, | |
float | sigma | |||
) | const |
Return a Gaussian random number.
[in] | mean | The gaussian mean |
[in] | sigma | The gaussian sigma |
Definition at line 172 of file randnum.cpp.
References get_frand_pos(), log(), r, sqrt(), x, and y.
Referenced by EMAN::Util::get_gauss_rand(), EMAN::TestImageNoiseGauss::process_inplace(), EMAN::AddRandomNoiseProcessor::process_inplace(), and EMAN::AddNoiseProcessor::process_inplace().
00173 { 00174 float x = 0.0f; 00175 float y = 0.0f; 00176 float r = 0.0f; 00177 bool valid = true; 00178 00179 while (valid) { 00180 x = get_frand_pos(-1.0, 1.0); 00181 y = get_frand_pos(-1.0, 1.0); 00182 r = x * x + y * y; 00183 00184 if (r <= 1.0 && r > 0) { 00185 valid = false; 00186 } 00187 } 00188 00189 float f = std::sqrt(-2.0f * std::log(r) / r); 00190 float result = x * f * sigma + mean; 00191 return result; 00192 }
long long Randnum::get_irand | ( | long long | lo, | |
long long | hi | |||
) | const |
This function returns a random integer from lo to hi inclusive.
All integers in the range [lo,hi] are produced with equal probability.
lo | the low end of the random integer | |
hi | the high end of the random integer |
Definition at line 157 of file randnum.cpp.
References r.
Referenced by EMAN::Util::get_irand().
00158 { 00159 return gsl_rng_uniform_int(r, hi-lo+1)+lo; 00160 }
unsigned long long Randnum::get_seed | ( | ) |
Get the current random number seed.
Definition at line 152 of file randnum.cpp.
References _seed.
Referenced by EMAN::Util::get_randnum_seed().
00153 { 00154 return _seed; 00155 }
Randnum * Randnum::Instance | ( | const gsl_rng_type * | _t | ) | [static] |
Definition at line 116 of file randnum.cpp.
References _instance, _seed, r, Randnum(), and T.
00116 { 00117 if(_instance == 0) { 00118 _instance = new Randnum(_t); 00119 } 00120 else if(_t != _instance->T) { 00121 gsl_rng_free (_instance->r); 00122 _instance->r = gsl_rng_alloc (_t); 00123 gsl_rng_set(_instance->r, _seed ); 00124 } 00125 00126 return _instance; 00127 }
Randnum * Randnum::Instance | ( | ) | [static] |
Definition at line 108 of file randnum.cpp.
References _instance, and Randnum().
Referenced by EMAN::Util::get_frand(), EMAN::Util::get_gauss_rand(), EMAN::Util::get_irand(), EMAN::Util::get_randnum_seed(), EMAN::TestImageNoiseGauss::process_inplace(), EMAN::TestImageNoiseUniformRand::process_inplace(), EMAN::AddRandomNoiseProcessor::process_inplace(), EMAN::AddNoiseProcessor::process_inplace(), and EMAN::Util::set_randnum_seed().
00108 { 00109 if(_instance == 0) { 00110 _instance = new Randnum(); 00111 } 00112 00113 return _instance; 00114 }
void Randnum::print_generator_type | ( | ) | const |
print out all possible random number generator type in gsl
Definition at line 194 of file randnum.cpp.
References t.
00195 { 00196 const gsl_rng_type **t, **t0; 00197 00198 t0 = gsl_rng_types_setup (); 00199 00200 printf ("Available generators:\n"); 00201 00202 for (t = t0; *t != 0; t++) { 00203 printf ("%s\n", (*t)->name); 00204 } 00205 }
void Randnum::set_seed | ( | unsigned long long | seed | ) |
Set the seed for the random number generator.
If the generator is seeded with the same value of s on two different runs, the same stream of random numbers will be generated by successive calls to the routines below. If different values of s are supplied, then the generated streams of random numbers should be completely different. If the seed s is zero then the standard seed from the original implementation is used instead.
seed | the seed for random number generator |
Definition at line 146 of file randnum.cpp.
Referenced by EMAN::TestImageNoiseGauss::process_inplace(), EMAN::TestImageNoiseUniformRand::process_inplace(), EMAN::AddRandomNoiseProcessor::process_inplace(), EMAN::AddNoiseProcessor::process_inplace(), and EMAN::Util::set_randnum_seed().
Randnum * Randnum::_instance = 0 [static, private] |
unsigned long long Randnum::_seed = random_seed() [static, private] |
Definition at line 171 of file randnum.h.
Referenced by get_seed(), Instance(), Randnum(), and set_seed().
gsl_rng * Randnum::r = 0 [static, private] |
Definition at line 170 of file randnum.h.
Referenced by get_frand(), get_frand_pos(), get_gauss_rand(), get_irand(), Instance(), Randnum(), set_seed(), and ~Randnum().
const gsl_rng_type * Randnum::T = gsl_rng_default [static, private] |