#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 | |
Randnum * | Instance () |
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 | |
const gsl_rng_type * | T = gsl_rng_default |
gsl_rng * | r = 0 |
unsigned long long | _seed = random_seed() |
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(); 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); *
Definition at line 90 of file randnum.h.
|
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(). 00130 { 00131 r = gsl_rng_alloc (T); 00132 gsl_rng_set(r, _seed ); 00133 }
|
|
|
|
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.
Definition at line 135 of file randnum.cpp. 00136 { 00137 r = gsl_rng_alloc (_t); 00138 gsl_rng_set(r, _seed ); 00139 }
|
|
Definition at line 141 of file randnum.cpp. References r. 00142 { 00143 gsl_rng_free (r); 00144 }
|
|
This function returns a random float from lo inclusive to hi.
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 }
|
|
This function returns a random float from lo to hi.
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 }
|
|
Return a Gaussian random number.
Definition at line 172 of file randnum.cpp. References get_frand_pos(), 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 }
|
|
This function returns a random integer from lo to hi inclusive. All integers in the range [lo,hi] are produced with equal probability.
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 }
|
|
Get the current random number seed.
Definition at line 152 of file randnum.cpp. Referenced by EMAN::Util::get_randnum_seed(). 00153 {
00154 return _seed;
00155 }
|
|
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 }
|
|
Definition at line 108 of file randnum.cpp. References _instance, and Randnum(). Referenced by EMAN::TestImageNoiseGauss::process_inplace(), EMAN::TestImageNoiseUniformRand::process_inplace(), EMAN::AddRandomNoiseProcessor::process_inplace(), and EMAN::AddNoiseProcessor::process_inplace(). 00108 { 00109 if(_instance == 0) { 00110 _instance = new Randnum(); 00111 } 00112 00113 return _instance; 00114 }
|
|
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 }
|
|
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.
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(). 00147 { 00148 _seed = seed; 00149 gsl_rng_set(r, _seed); 00150 }
|
|
Definition at line 103 of file randnum.cpp. Referenced by Instance(). |
|
Definition at line 106 of file randnum.cpp. Referenced by Instance(), Randnum(), and set_seed(). |
|
Definition at line 105 of file randnum.cpp. Referenced by get_frand(), get_frand_pos(), get_gauss_rand(), get_irand(), Instance(), Randnum(), set_seed(), and ~Randnum(). |
|
Definition at line 104 of file randnum.cpp. Referenced by Instance(), and Randnum(). |