Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File 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]
List of all members.

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

RandnumInstance ()
RandnumInstance (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

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.

Definition at line 90 of file randnum.h.


Constructor & Destructor Documentation

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.

References _seed, r, and T.

Referenced by Instance().

00130 {
00131         r = gsl_rng_alloc (T);  
00132         gsl_rng_set(r, _seed ); 
00133 }

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.

Parameters:
_t the random number generator type

Definition at line 135 of file randnum.cpp.

References _seed, and r.

00136 {
00137         r = gsl_rng_alloc (_t); 
00138         gsl_rng_set(r, _seed );
00139 }

Randnum::~Randnum  )  [protected]
 

Definition at line 141 of file randnum.cpp.

References r.

00142 {
00143         gsl_rng_free (r);
00144 }


Member Function Documentation

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:
lo the low end of the random float
hi the high end of the random float
Returns:
the float in [lo, hi), default in [0,0,1.0)

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.

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

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.

Parameters:
[in] mean The gaussian mean
[in] sigma The gaussian sigma
Returns:
the gaussian random number in float.

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 }

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:
lo the low end of the random integer
hi the high end of the random integer
Returns:
the long integer in [lo, hi]

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.

Returns:
current seed

Definition at line 152 of file randnum.cpp.

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::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 }

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.

Parameters:
seed the seed for random number generator

Definition at line 146 of file randnum.cpp.

References _seed, and r.

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 }


Member Data Documentation

Randnum * Randnum::_instance = 0 [static, private]
 

Definition at line 103 of file randnum.cpp.

Referenced by Instance().

unsigned long long Randnum::_seed = random_seed() [static, private]
 

Definition at line 106 of file randnum.cpp.

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

gsl_rng * Randnum::r = 0 [static, private]
 

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().

const gsl_rng_type * Randnum::T = gsl_rng_default [static, private]
 

Definition at line 104 of file randnum.cpp.

Referenced by Instance(), and Randnum().


The documentation for this class was generated from the following files:
Generated on Tue Jun 11 13:42:53 2013 for EMAN2 by  doxygen 1.3.9.1