EMAN2
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Private Attributes | List of all members
EMAN::Randnum Class Reference

The wrapper class for gsl's random number generater. More...

#include <randnum.h>

Collaboration diagram for EMAN::Randnum:
Collaboration graph
[legend]

Public Member Functions

void set_seed (unsigned long long seed)
 Set the seed for the random number generator. More...
 
unsigned long long get_seed ()
 Get the current random number seed. More...
 
long long get_irand (long long lo, long long hi) const
 This function returns a random integer from lo to hi inclusive. More...
 
float get_frand (double lo=0.0, double hi=1.0) const
 This function returns a random float from lo inclusive to hi. More...
 
float get_frand_pos (double lo=0.0, double hi=1.0) const
 This function returns a random float from lo to hi. More...
 
float get_gauss_rand (float mean, float sigma) const
 Return a Gaussian random number. More...
 
void print_generator_type () const
 print out all possible random number generator type in gsl More...
 

Static Public Member Functions

static RandnumInstance ()
 
static RandnumInstance (const gsl_rng_type *_t)
 

Protected Member Functions

 Randnum ()
 The default constructor will use the gsl default random number generator gal_rng_mt19937. More...
 
 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. More...
 
 ~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
 

Detailed Description

The wrapper class for gsl's random number generater.

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.

 - How to get a random integer in range [lo, hi]
int i = r->get_irand(lo, hi);
static Randnum * Instance()
Definition: randnum.cpp:104
Randnum()
The default constructor will use the gsl default random number generator gal_rng_mt19937.
Definition: randnum.cpp:125
static gsl_rng * r
Definition: randnum.h:166

Definition at line 86 of file randnum.h.

Constructor & Destructor Documentation

◆ Randnum() [1/3]

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 125 of file randnum.cpp.

126{
127 r = gsl_rng_alloc (T);
128 gsl_rng_set(r, _seed );
129}
static unsigned long long _seed
Definition: randnum.h:167
static const gsl_rng_type * T
Definition: randnum.h:165

References _seed, r, and T.

Referenced by Instance().

◆ Randnum() [2/3]

EMAN::Randnum::Randnum ( const Randnum )
protected

◆ Randnum() [3/3]

Randnum::Randnum ( const gsl_rng_type *  _t)
explicitprotected

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.

Parameters
_tthe random number generator type

Definition at line 131 of file randnum.cpp.

132{
133 r = gsl_rng_alloc (_t);
134 gsl_rng_set(r, _seed );
135}

References _seed, and r.

◆ ~Randnum()

Randnum::~Randnum ( )
protected

Definition at line 137 of file randnum.cpp.

138{
139 gsl_rng_free (r);
140}

References r.

Member Function Documentation

◆ get_frand()

float Randnum::get_frand ( double  lo = 0.0,
double  hi = 1.0 
) const

This function returns a random float from lo inclusive to hi.

Parameters
lothe low end of the random float
hithe high end of the random float
Returns
the float in [lo, hi), default in [0,0,1.0)

Definition at line 158 of file randnum.cpp.

159{
160 return static_cast<float>(gsl_rng_uniform(r) * (hi -lo) + lo);
161}

References r.

Referenced by EMAN::Util::get_frand(), EMAN::AddRandomNoiseProcessor::process_inplace(), and EMAN::TestImageNoiseUniformRand::process_inplace().

◆ get_frand_pos()

float Randnum::get_frand_pos ( double  lo = 0.0,
double  hi = 1.0 
) const

This function returns a random float from lo to hi.

Parameters
lothe low end of the random float
hithe high end of the random float
Returns
the float in (lo, hi), default in (0,0,1.0)

Definition at line 163 of file randnum.cpp.

164{
165 return static_cast<float>(gsl_rng_uniform_pos(r) * (hi -lo) + lo);
166}

References r.

Referenced by get_gauss_rand().

◆ get_gauss_rand()

float Randnum::get_gauss_rand ( float  mean,
float  sigma 
) const

Return a Gaussian random number.

Parameters
[in]meanThe gaussian mean
[in]sigmaThe gaussian sigma
Returns
the gaussian random number in float.

Definition at line 168 of file randnum.cpp.

169{
170 float x = 0.0f;
171 float y = 0.0f;
172 float r = 0.0f;
173 bool valid = true;
174
175 while (valid) {
176 x = get_frand_pos(-1.0, 1.0);
177 y = get_frand_pos(-1.0, 1.0);
178 r = x * x + y * y;
179
180 if (r <= 1.0 && r > 0) {
181 valid = false;
182 }
183 }
184
185 float f = std::sqrt(-2.0f * std::log(r) / r);
186 float result = x * f * sigma + mean;
187 return result;
188}
float get_frand_pos(double lo=0.0, double hi=1.0) const
This function returns a random float from lo to hi.
Definition: randnum.cpp:163
EMData * log() const
return natural logarithm image for a image
EMData * sqrt() const
return square root of current image
#define y(i, j)
Definition: projector.cpp:1516
#define x(i)
Definition: projector.cpp:1517

References get_frand_pos(), log(), r, sqrt(), x, and y.

Referenced by EMAN::Util::get_gauss_rand(), EMAN::AddNoiseProcessor::process_inplace(), EMAN::AddRandomNoiseProcessor::process_inplace(), and EMAN::TestImageNoiseGauss::process_inplace().

◆ get_irand()

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.

Parameters
lothe low end of the random integer
hithe high end of the random integer
Returns
the long integer in [lo, hi]

Definition at line 153 of file randnum.cpp.

154{
155 return gsl_rng_uniform_int(r, hi-lo+1)+lo;
156}

References r.

Referenced by EMAN::Util::get_irand().

◆ get_seed()

unsigned long long Randnum::get_seed ( )

Get the current random number seed.

Returns
current seed

Definition at line 148 of file randnum.cpp.

149{
150 return _seed;
151}

References _seed.

Referenced by EMAN::Util::get_randnum_seed().

◆ Instance() [1/2]

Randnum * Randnum::Instance ( )
static

◆ Instance() [2/2]

Randnum * Randnum::Instance ( const gsl_rng_type *  _t)
static

Definition at line 112 of file randnum.cpp.

112 {
113 if(_instance == 0) {
114 _instance = new Randnum(_t);
115 }
116 else if(_t != _instance->T) {
117 gsl_rng_free (_instance->r);
118 _instance->r = gsl_rng_alloc (_t);
119 gsl_rng_set(_instance->r, _seed );
120 }
121
122 return _instance;
123}

References _instance, _seed, r, Randnum(), and T.

◆ print_generator_type()

void Randnum::print_generator_type ( ) const

print out all possible random number generator type in gsl

Definition at line 190 of file randnum.cpp.

191{
192 const gsl_rng_type **t, **t0;
193
194 t0 = gsl_rng_types_setup ();
195
196 printf ("Available generators:\n");
197
198 for (t = t0; *t != 0; t++) {
199 printf ("%s\n", (*t)->name);
200 }
201}

◆ set_seed()

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.

Parameters
seedthe seed for random number generator

Definition at line 142 of file randnum.cpp.

143{
144 _seed = seed;
145 gsl_rng_set(r, _seed);
146}

References _seed, and r.

Referenced by EMAN::AddNoiseProcessor::process_inplace(), EMAN::AddRandomNoiseProcessor::process_inplace(), EMAN::TestImageNoiseUniformRand::process_inplace(), EMAN::TestImageNoiseGauss::process_inplace(), and EMAN::Util::set_randnum_seed().

Member Data Documentation

◆ _instance

Randnum * Randnum::_instance = 0
staticprivate

Definition at line 169 of file randnum.h.

Referenced by Instance().

◆ _seed

unsigned long long Randnum::_seed = random_seed()
staticprivate

Definition at line 167 of file randnum.h.

Referenced by get_seed(), Instance(), Randnum(), and set_seed().

◆ r

gsl_rng * Randnum::r = 0
staticprivate

◆ T

const gsl_rng_type * Randnum::T = gsl_rng_default
staticprivate

Definition at line 165 of file randnum.h.

Referenced by Instance(), and Randnum().


The documentation for this class was generated from the following files: