EMAN2
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Private Attributes
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

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

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

{
        r = gsl_rng_alloc (T);  
        gsl_rng_set(r, _seed ); 
}
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:
_tthe random number generator type

Definition at line 135 of file randnum.cpp.

References _seed, and r.

{
        r = gsl_rng_alloc (_t); 
        gsl_rng_set(r, _seed );
}
Randnum::~Randnum ( ) [protected]

Definition at line 141 of file randnum.cpp.

References r.

{
        gsl_rng_free (r);
}

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

References r.

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

{
        return static_cast<float>(gsl_rng_uniform(r) * (hi -lo) + lo);
}
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 167 of file randnum.cpp.

References r.

Referenced by get_gauss_rand().

{
        return static_cast<float>(gsl_rng_uniform_pos(r) * (hi -lo) + lo);
}
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 172 of file randnum.cpp.

References get_frand_pos(), log(), mean(), 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().

{
        float x = 0.0f;
        float y = 0.0f;
        float r = 0.0f;
        bool valid = true;
        
        while (valid) {
                x = get_frand_pos(-1.0, 1.0);
                y = get_frand_pos(-1.0, 1.0);
                r = x * x + y * y;
                
                if (r <= 1.0 && r > 0) {
                        valid = false;
                }
        }
        
        float f = std::sqrt(-2.0f * std::log(r) / r);
        float result = x * f * sigma + mean;
        return result;
}
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 157 of file randnum.cpp.

References r.

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

{
        return gsl_rng_uniform_int(r, hi-lo+1)+lo;
}
unsigned long long Randnum::get_seed ( )

Get the current random number seed.

Returns:
current seed

Definition at line 152 of file randnum.cpp.

References _seed.

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

{
        return _seed;
}
Randnum * Randnum::Instance ( ) [static]
Randnum * Randnum::Instance ( const gsl_rng_type *  _t) [static]

Definition at line 116 of file randnum.cpp.

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

                                                   {
        if(_instance == 0) {
                _instance = new Randnum(_t);
        }
        else if(_t != _instance->T) {
                gsl_rng_free (_instance->r);
                _instance->r = gsl_rng_alloc (_t);
                gsl_rng_set(_instance->r, _seed );
        }
        
        return _instance;
}
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.

{
        const gsl_rng_type **t, **t0;
          
        t0 = gsl_rng_types_setup ();
          
        printf ("Available generators:\n");
          
        for (t = t0; *t != 0; t++) {
                printf ("%s\n", (*t)->name);
        }       
}
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 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().

{
        _seed = seed;
        gsl_rng_set(r, _seed);
}

Member Data Documentation

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

Definition at line 173 of file randnum.h.

Referenced by Instance().

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]
const gsl_rng_type * Randnum::T = gsl_rng_default [static, private]

Definition at line 169 of file randnum.h.

Referenced by Instance(), and Randnum().


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