PLearn 0.1
|
Important note: the pointers used for some Boost distribution objects are meant to save memory. More...
#include <PRandom.h>
Public Member Functions | |
PRandom (int32_t seed=-1) | |
Constructor from a given seed. | |
PRandom (const PRandom &rhs) | |
Copy constructor. | |
PRandom | operator= (const PRandom &rhs) |
Operator= . | |
virtual | ~PRandom () |
Destructor to free memory. | |
const boost::mt19937 * | get_rgen () const |
Accessors. | |
boost::exponential_distribution * | get_exponential_distribution () const |
boost::normal_distribution * | get_normal_distribution () const |
boost::uniform_01 < boost::mt19937 > * | get_uniform_01 () const |
uint32_t | get_the_seed () const |
int32_t | get_fixed_seed () const |
int32_t | get_seed () const |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual PRandom * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Build object. | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
void | manual_seed (int32_t x) |
Set the 'seed' option to 'x' and reset the random number generator accordingly. | |
void | time_seed () |
Initialize the random number generator with the CPU time. | |
real | uniform_sample () |
Return a random number uniformly distributed between 0 and 1. | |
real | bounded_uniform (real a, real b) |
Return a random number uniformly distributed between a and b. | |
real | gaussian_01 () |
Return a random number generated from a Gaussian with mean 0 and stddev 1. | |
real | normal_sample () |
real | gaussian_mu_sigma (real mu, real sigma) |
Return a random number generated from a Gaussian with mean mu and stddev sigma. | |
void | fill_random_discrete (const Vec &dest, const Vec &set) |
Fill vector 'dest' with samples taken randomly from the given 'set'. | |
void | fill_random_normal (const Vec &dest, real mean=0, real stddev=1) |
Fill vector 'dest' with samples generated from a normal distribution with mean 'mean' and standard deviation 'stddev'. | |
void | fill_random_normal (const Mat &dest, real mean=0, real stddev=1) |
Fill matrix 'dest' with samples generated from a normal distribution with mean 'mean' and standard deviation 'stddev'. | |
void | fill_random_uniform (const Vec &dest, real min=0, real max=1) |
Fill vector 'dest' with samples generated from a uniform distribution between 'minval' and 'maxval'. | |
void | fill_random_uniform (const Mat &dest, real min=0, real max=1) |
Fill matrix 'dest' with samples generated from a uniform distribution between 'minval' and 'maxval'. | |
real | exp_sample () |
Return a random number generated from an exponential distribution with parameter lambda = 1. | |
int | multinomial_sample (const Vec &distribution) |
Return a random deviate from a discrete distribution given explicitely in the 'distribution' vector. | |
int | uniform_multinomial_sample (int n) |
Return an integer between 0 and n-1 with equal probabilities. | |
int | binomial_sample (real pp) |
Return a sample from a binomial, given the expectation 'pp'. | |
template<class T > | |
void | shuffleElements (const TVec< T > &vec) |
Randomly shuffle the entries of a vector (random permutation) | |
template<class T > | |
TVec< T > | weightedShuffleElements (const TVec< T > &vec, const Vec &weights, int number_to_keep=-1) |
Return a weighted random permutation of the entries of a vector. | |
template<class T > | |
void | shuffleRows (const TMat< T > &mat) |
Randomly shuffle the rows of a matrix. | |
PP< PRandom > | split () |
Return a newly allocated PRandom from this one; The new PRandom is seeded with this one's output. | |
Static Public Member Functions | |
static string | _classname_ () |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static Object * | _new_instance_for_typemap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
static PP< PRandom > | common (bool random_seed=true) |
Return a pointer to a common PRandom object accessible from any PLearn code. | |
Public Attributes | |
int32_t | seed_ |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
void | time_seed_ () |
Initialize the random number generator with the CPU time. | |
void | manual_seed_ (int32_t x) |
Initialize the random number generator with the given int32_t 'x'. | |
void | ensure_uniform_01 () |
Ensure the 'uniform_01' member is correctly initialized. | |
void | ensure_normal_distribution () |
Ensure the 'normal_distribution' member is correctly initialized. | |
void | ensure_exponential_distribution () |
Ensure the 'exponential_distribution' member is correctly initialized. | |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
Protected Attributes | |
boost::mt19937 | rgen |
The underlying Boost random number generator used. | |
boost::exponential_distribution * | exponential_distribution |
The underlying Boost distribution for exponential sampling. | |
boost::normal_distribution * | normal_distribution |
The underlying Boost distribution for normal sampling. | |
boost::uniform_01 < boost::mt19937 > * | uniform_01 |
The underlying Boost distribution for uniform sampling. | |
uint32_t | the_seed |
The actual seed used by the random number generator. | |
int32_t | fixed_seed |
Private Types | |
typedef Object | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Important note: the pointers used for some Boost distribution objects are meant to save memory.
They should not be set outside of this class, nor should they be used by any other object (as they will be destroyed with this object).
Note also that any field added to this object should be properly copied (and possibly deep-copied) in the copy constructor.
typedef Object PLearn::PRandom::inherited [private] |
Reimplemented from PLearn::Object.
PLearn::PRandom::PRandom | ( | int32_t | seed = -1 | ) |
Constructor from a given seed.
Definition at line 56 of file PRandom.cc.
References build().
Referenced by common(), and split().
: #ifdef BOUNDCHECK samples_count(0), #endif exponential_distribution(0), normal_distribution(0), uniform_01(0), the_seed(0), fixed_seed(0), seed_(seed) { // For convenience, we systematically call build() in the constructor. build(); }
PLearn::PRandom::PRandom | ( | const PRandom & | rhs | ) |
Copy constructor.
This constructor ensures that no deep-copy is needed. All fields are properly [deep-]copied in this constructor.
Definition at line 71 of file PRandom.cc.
References exponential_distribution, get_exponential_distribution(), get_normal_distribution(), get_uniform_01(), normal_distribution, and uniform_01.
: #ifdef BOUNDCHECK samples_count (rhs.get_samples_count()), #endif rgen (*(rhs.get_rgen())), the_seed (rhs.get_the_seed()), fixed_seed (rhs.get_fixed_seed()), seed_ (rhs.get_seed()) { // Note: the extra parentheses are here to tell the compiler that the // assignments are meant to be used as truth values. if ((exponential_distribution = rhs.get_exponential_distribution())) exponential_distribution = new boost::exponential_distribution<> (*exponential_distribution); if ((normal_distribution = rhs.get_normal_distribution())) normal_distribution = new boost::normal_distribution<> (*normal_distribution); if ((uniform_01 = rhs.get_uniform_01())) uniform_01 = new boost::uniform_01<boost::mt19937> (*uniform_01); }
PLearn::PRandom::~PRandom | ( | ) | [virtual] |
Destructor to free memory.
Definition at line 413 of file PRandom.cc.
References exponential_distribution, normal_distribution, and uniform_01.
{ if (uniform_01) { delete uniform_01; uniform_01 = 0; } if (normal_distribution) { delete normal_distribution; normal_distribution = 0; } if (exponential_distribution) { delete exponential_distribution; exponential_distribution = 0; } }
string PLearn::PRandom::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 120 of file PRandom.cc.
OptionList & PLearn::PRandom::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 120 of file PRandom.cc.
RemoteMethodMap & PLearn::PRandom::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 120 of file PRandom.cc.
Reimplemented from PLearn::Object.
Definition at line 120 of file PRandom.cc.
Object * PLearn::PRandom::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 120 of file PRandom.cc.
StaticInitializer PRandom::_static_initializer_ & PLearn::PRandom::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 120 of file PRandom.cc.
Return a sample from a binomial, given the expectation 'pp'.
Definition at line 351 of file PRandom.cc.
References PLERROR, u, and uniform_sample().
{ if( pp < 0 || pp > 1 ) PLERROR("In PRandom::binomial_sample, pp should be between 0 and 1, " "but is %f.", pp); real u = this->uniform_sample(); if( pp < u ) return 0; else return 1; }
Return a random number uniformly distributed between a and b.
Definition at line 148 of file PRandom.cc.
References a, RAND_RNMX, and uniform_sample().
Referenced by fill_random_uniform().
{ real res = uniform_sample()*(b-a) + a; if (res >= b) return b*RAND_RNMX; else return res; }
void PLearn::PRandom::build | ( | ) | [virtual] |
Build object.
Note: one should not have to call build() on a PRandom object, as it is called directly in the constructor and in the time_seed() and manual_seed() methods. Don't forget that calling build() will re-initialize the random number generator.
Reimplemented from PLearn::Object.
Definition at line 159 of file PRandom.cc.
References PLearn::Object::build(), and build_().
Referenced by manual_seed(), and PRandom().
{ inherited::build(); build_(); }
void PLearn::PRandom::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::Object.
Definition at line 168 of file PRandom.cc.
References fixed_seed, manual_seed_(), PLERROR, seed_, and time_seed_().
Referenced by build().
{ if (fixed_seed) { if (fixed_seed == -2) fixed_seed = seed_; else { if (seed_ != fixed_seed) PLERROR("In PRandom::build_ - You are not allowed to modify the seed of " "a PRandom object whose seed has been fixed"); } } if (seed_ == -1) this->time_seed_(); else if (seed_ == 0) {} else if (seed_ > 0) this->manual_seed_(seed_); else PLERROR("In PRandom::build_ - The only value allowed for the seed are " "-1, 0 or a strictly positive int32_t integer"); }
string PLearn::PRandom::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 120 of file PRandom.cc.
Return a pointer to a common PRandom object accessible from any PLearn code.
There are two common PRandom objects: one whose seed was given by the CPU time (no argument, or 'random_seed' set to true), and another whose seed is fixed at compilation time ('random_seed' set to false). The latter can be useful to get reproducible results. For safety, it is not possible to change their seed.
Definition at line 192 of file PRandom.cc.
References PRandom().
Referenced by PLearn::ThresholdedKernel::evaluate_random_k_x_i(), PLearn::SaltPepperNoiseVariable::fprop(), PLearn::RandomForcedValuesVariable::fprop(), PLearn::MultiSampleVariable::fprop(), PLearn::BernoulliSampleVariable::fprop(), PLearn::AdditiveGaussianNoiseVariable::fprop(), PLearn::VarUtilsTest::perform(), PLearn::VariablesTest::perform(), PLearn::TMatTest::perform(), PLearn::PLMathTest::perform(), PLearn::VerifyGradientCommand::run(), PLearn::ThresholdedKernel::setDataForKernelMatrix(), and PLearn::GaussMix::train().
{ static PP<PRandom> gen_random = 0; static PP<PRandom> gen_const = 0; if (random_seed) { if (!gen_random) { gen_random = new PRandom(); gen_random->fixed_seed = -2; gen_random->build(); } return gen_random; } else { if (!gen_const) { gen_const = new PRandom(12345678); gen_const->fixed_seed = -2; gen_const->build(); } return gen_const; } }
void PLearn::PRandom::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::Object.
Definition at line 125 of file PRandom.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::Object::declareOptions(), fixed_seed, PLearn::OptionBase::learntoption, and seed_.
{ declareOption(ol, "seed", &PRandom::seed_, OptionBase::buildoption, "Seed for the random number generator, set at build time:\n" " - -1 : initialized with the current CPU time\n" " - 0 : the current seed is left intact\n" " - x > 0 : the seed is changed to 'x'"); // Declared as a learnt option to hide some complexity to the novice. declareOption(ol, "fixed_seed", &PRandom::fixed_seed, OptionBase::learntoption, "If set to 0, will be ignored. If set to -2, its value will be copied from\n" "the 'seed' option. If set to any other value, it must always be equal to\n" "'seed' when build() is called. This allows one to prevent the seed from\n" "being accidentally modified by setting 'fixed_seed' to -2. Someone modifying\n" "the seed afterwards will then get an error.\n"); // Now call the parent class' declareOptions inherited::declareOptions(ol); }
static const PPath& PLearn::PRandom::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::Object.
Definition at line 120 of file PRandom.cc.
void PLearn::PRandom::ensure_exponential_distribution | ( | ) | [inline, protected] |
Ensure the 'exponential_distribution' member is correctly initialized.
This method should be called before using the 'exponential_distribution' member, as its presence is not always guaranteed.
Definition at line 184 of file PRandom.h.
References exponential_distribution.
Referenced by exp_sample().
{ if (!exponential_distribution) exponential_distribution = new boost::exponential_distribution<>(); }
void PLearn::PRandom::ensure_normal_distribution | ( | ) | [inline, protected] |
Ensure the 'normal_distribution' member is correctly initialized.
This method should be called before using the 'normal_distribution' member, as its presence is not always guaranteed.
Definition at line 176 of file PRandom.h.
References normal_distribution.
Referenced by gaussian_01().
{ if (!normal_distribution) normal_distribution = new boost::normal_distribution<>(); }
void PLearn::PRandom::ensure_uniform_01 | ( | ) | [inline, protected] |
Ensure the 'uniform_01' member is correctly initialized.
This method is called in build(), so it should not be needed to call it from anywhere else.
Definition at line 168 of file PRandom.h.
References rgen, and uniform_01.
Referenced by manual_seed_().
{ if (!uniform_01) uniform_01 = new boost::uniform_01<boost::mt19937>(rgen); }
real PLearn::PRandom::exp_sample | ( | ) |
Return a random number generated from an exponential distribution with parameter lambda = 1.
Definition at line 216 of file PRandom.cc.
References ensure_exponential_distribution(), exponential_distribution, and uniform_01.
{ ensure_exponential_distribution(); #ifdef BOUNDCHECK samples_count++; #endif return real((*exponential_distribution)(*uniform_01)); }
Fill vector 'dest' with samples taken randomly from the given 'set'.
Definition at line 227 of file PRandom.cc.
References PLearn::TVec< T >::begin(), PLearn::TVec< T >::end(), PLearn::TVec< T >::isEmpty(), n, PLASSERT, and uniform_multinomial_sample().
{ PLASSERT( dest.isEmpty() || !set.isEmpty() ); Vec::iterator it = dest.begin(); Vec::iterator itend = dest.end(); int n = set.length(); for(; it != itend; ++it) *it = set[this->uniform_multinomial_sample(n)]; }
Fill vector 'dest' with samples generated from a normal distribution with mean 'mean' and standard deviation 'stddev'.
Definition at line 240 of file PRandom.cc.
References gaussian_mu_sigma(), i, and PLearn::TVec< T >::length().
Referenced by fill_random_normal().
{ for (int i = 0; i < dest.length(); i++) dest[i] = gaussian_mu_sigma(mean, stddev); }
Fill matrix 'dest' with samples generated from a normal distribution with mean 'mean' and standard deviation 'stddev'.
Definition at line 245 of file PRandom.cc.
References fill_random_normal(), i, PLearn::TMat< T >::length(), and PLearn::mean().
Fill matrix 'dest' with samples generated from a uniform distribution between 'minval' and 'maxval'.
Definition at line 259 of file PRandom.cc.
References fill_random_uniform(), i, PLearn::TMat< T >::length(), PLearn::max(), and PLearn::min().
Fill vector 'dest' with samples generated from a uniform distribution between 'minval' and 'maxval'.
Definition at line 253 of file PRandom.cc.
References bounded_uniform(), i, and PLearn::TVec< T >::length().
Referenced by fill_random_uniform().
{ for (int i = 0; i < dest.length(); i++) dest[i] = bounded_uniform(min, max); }
real PLearn::PRandom::gaussian_01 | ( | ) |
Return a random number generated from a Gaussian with mean 0 and stddev 1.
Definition at line 268 of file PRandom.cc.
References ensure_normal_distribution(), normal_distribution, and uniform_01.
Referenced by gaussian_mu_sigma(), and normal_sample().
{ ensure_normal_distribution(); #ifdef BOUNDCHECK // Drawing one Gaussian sample has the same effect on the underlying // generator as drawing two uniform or exponential samples samples_count += 2; #endif return real((*normal_distribution)(*uniform_01)); }
Return a random number generated from a Gaussian with mean mu and stddev sigma.
Definition at line 281 of file PRandom.cc.
References gaussian_01().
Referenced by fill_random_normal().
{ return gaussian_01() * sigma + mu; }
boost::exponential_distribution* PLearn::PRandom::get_exponential_distribution | ( | ) | const [inline] |
Definition at line 133 of file PRandom.h.
References exponential_distribution.
Referenced by operator=(), and PRandom().
{ return exponential_distribution; }
int32_t PLearn::PRandom::get_fixed_seed | ( | ) | const [inline] |
Definition at line 141 of file PRandom.h.
References fixed_seed.
Referenced by operator=().
{ return fixed_seed; }
boost::normal_distribution* PLearn::PRandom::get_normal_distribution | ( | ) | const [inline] |
Definition at line 135 of file PRandom.h.
References normal_distribution.
Referenced by operator=(), and PRandom().
{ return normal_distribution; }
const boost::mt19937* PLearn::PRandom::get_rgen | ( | ) | const [inline] |
Accessors.
Definition at line 131 of file PRandom.h.
References rgen.
Referenced by operator=().
{ return &rgen; }
int32_t PLearn::PRandom::get_seed | ( | ) | const [inline] |
Definition at line 142 of file PRandom.h.
References seed_.
Referenced by operator=().
{ return seed_; }
uint32_t PLearn::PRandom::get_the_seed | ( | ) | const [inline] |
Definition at line 140 of file PRandom.h.
References the_seed.
Referenced by operator=().
{ return the_seed; }
boost::uniform_01<boost::mt19937>* PLearn::PRandom::get_uniform_01 | ( | ) | const [inline] |
Definition at line 137 of file PRandom.h.
References uniform_01.
Referenced by operator=(), and PRandom().
{ return uniform_01; }
OptionList & PLearn::PRandom::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 120 of file PRandom.cc.
OptionMap & PLearn::PRandom::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 120 of file PRandom.cc.
RemoteMethodMap & PLearn::PRandom::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 120 of file PRandom.cc.
void PLearn::PRandom::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::Object.
Definition at line 288 of file PRandom.cc.
References PLearn::Object::makeDeepCopyFromShallowCopy().
{ inherited::makeDeepCopyFromShallowCopy(copies); // Nothing more should be added here: this object is meant to be properly // copied directly within the copy constructor (this is where you should add // any statement needed for a proper copy). }
void PLearn::PRandom::manual_seed | ( | int32_t | x | ) |
Set the 'seed' option to 'x' and reset the random number generator accordingly.
'x' may be -1 to initialize from the current CPU time, or 0 to make no initialization.
Definition at line 299 of file PRandom.cc.
References build(), fixed_seed, PLERROR, seed_, and x.
Referenced by time_seed().
{ if (fixed_seed) PLERROR("In PRandom::manual_seed - You are not allowed to change the seed " "of a PRandom object whose seed is fixed"); seed_ = x; build(); }
void PLearn::PRandom::manual_seed_ | ( | int32_t | x | ) | [protected] |
Initialize the random number generator with the given int32_t 'x'.
This is an internal method that does not update the 'seed' option.
Definition at line 311 of file PRandom.cc.
References ensure_uniform_01(), rgen, the_seed, and uniform_01.
Referenced by build_(), and time_seed_().
{ the_seed = uint32_t(x); rgen.seed(the_seed); if (uniform_01) { // The boost::uniform_01 object must be re-constructed from the updated // random number generator. delete uniform_01; uniform_01 = 0; } // Systematically construct the uniform_01 member, which is the basis for most // of the random operations. ensure_uniform_01(); #ifdef BOUNDCHECK samples_count = 0; #endif }
Return a random deviate from a discrete distribution given explicitely in the 'distribution' vector.
The returned value is an index in 'distribution'. Elements of 'distribution' must sum to 1.
Definition at line 332 of file PRandom.cc.
References PLearn::TVec< T >::data(), i, PLearn::TVec< T >::length(), n, u, and uniform_sample().
Referenced by weightedShuffleElements().
{ real u = this->uniform_sample(); real* pi = distribution.data(); real s = *pi; int n = distribution.length(); int i = 0; while ((i<n) && (s<u)) { i++; pi++; s += *pi; } if (i == n) i = n - 1; // Improbable, but... return i; }
real PLearn::PRandom::normal_sample | ( | ) | [inline] |
Definition at line 220 of file PRandom.h.
References gaussian_01().
{ return this->gaussian_01(); }
Operator= .
Do the same thing as copy constructor.
Definition at line 93 of file PRandom.cc.
References exponential_distribution, fixed_seed, get_exponential_distribution(), get_fixed_seed(), get_normal_distribution(), get_rgen(), get_seed(), get_the_seed(), get_uniform_01(), normal_distribution, rgen, seed_, the_seed, and uniform_01.
{ #ifdef BOUNDCHECK samples_count = rhs.get_samples_count(); #endif rgen = *(rhs.get_rgen()); the_seed = rhs.get_the_seed(); fixed_seed = rhs.get_fixed_seed(); seed_ = rhs.get_seed(); if ((exponential_distribution = rhs.get_exponential_distribution())) exponential_distribution = new boost::exponential_distribution<> (*exponential_distribution); if ((normal_distribution = rhs.get_normal_distribution())) normal_distribution = new boost::normal_distribution<> (*normal_distribution); if ((uniform_01 = rhs.get_uniform_01())) uniform_01 = new boost::uniform_01<boost::mt19937> (*uniform_01); return (*this); }
void PLearn::PRandom::shuffleElements | ( | const TVec< T > & | vec | ) |
Randomly shuffle the entries of a vector (random permutation)
Definition at line 313 of file PRandom.h.
References PLearn::TVec< T >::data(), i, PLearn::TVec< T >::isEmpty(), j, PLearn::TVec< T >::length(), n, and uniform_multinomial_sample().
Referenced by PLearn::DeepReconstructorNet::computeAndSaveLayerActivationStats(), and PLearn::TrainTestSplitter::getRandomSubsets().
{ if (vec.isEmpty()) return; T* v = vec.data(); T tmp; int n = vec.length(); for (int i = 0; i < vec.length(); i++) { int j = i + this->uniform_multinomial_sample(n - i); tmp = v[i]; v[i] = v[j]; v[j] = tmp; } }
void PLearn::PRandom::shuffleRows | ( | const TMat< T > & | mat | ) |
Randomly shuffle the rows of a matrix.
Definition at line 377 of file PRandom.h.
References i, j, PLearn::TMat< T >::length(), n, PLearn::TMat< T >::swapRows(), and uniform_sample().
{ int n = mat.length(); for (int i = 0; i < n; i++) { int j = i + int(this->uniform_sample() * (n - i)); mat.swapRows(i,j); } }
Return a newly allocated PRandom from this one; The new PRandom is seeded with this one's output.
Definition at line 393 of file PRandom.cc.
References PRandom(), and rgen.
{ // Draw twice from underlying rgen, // just in case it outputs its state // directly... (any better ideas?) int s; do { //no need for a specific distribution s= rgen() + rgen(); if(s < 0) s= -s; } while(s == 0); return new PRandom(s); }
void PLearn::PRandom::time_seed | ( | ) | [inline] |
Initialize the random number generator with the CPU time.
Definition at line 211 of file PRandom.h.
References manual_seed().
{ manual_seed(-1); }
void PLearn::PRandom::time_seed_ | ( | ) | [protected] |
Initialize the random number generator with the CPU time.
This is an internal method that does not update the 'seed' option.
Definition at line 366 of file PRandom.cc.
References manual_seed_().
Referenced by build_().
{ time_t ltime; struct tm *today; time(<ime); today = localtime(<ime); manual_seed_((int32_t)today->tm_sec+ 60*today->tm_min+ 60*60*today->tm_hour+ 60*60*24*today->tm_mday); }
Return an integer between 0 and n-1 with equal probabilities.
Definition at line 265 of file PRandom.h.
References uniform_sample().
Referenced by fill_random_discrete(), and shuffleElements().
{ return int(n * this->uniform_sample()); }
real PLearn::PRandom::uniform_sample | ( | ) |
Return a random number uniformly distributed between 0 and 1.
Definition at line 381 of file PRandom.cc.
References uniform_01.
Referenced by binomial_sample(), bounded_uniform(), multinomial_sample(), shuffleRows(), and uniform_multinomial_sample().
{ #ifdef BOUNDCHECK samples_count++; #endif return real((*uniform_01)()); }
TVec< T > PLearn::PRandom::weightedShuffleElements | ( | const TVec< T > & | vec, |
const Vec & | weights, | ||
int | number_to_keep = -1 |
||
) |
Return a weighted random permutation of the entries of a vector.
The 'weights' cause some elements to be picked first. It is not necessary for them to be normalized (the function takes care of it). If desired, only the first N elements of the permutation may be returned (faster).
Definition at line 332 of file PRandom.h.
References PLearn::TVec< T >::copy(), i, j, multinomial_sample(), PLASSERT, PLearn::TVec< T >::size(), PLearn::sum(), and w.
{ PLASSERT( vec.size() == weights.size() ); if (number_to_keep < 0) number_to_keep = vec.size(); Vec w = weights.copy(); TVec<T> r(number_to_keep); // Normalize the weights real s = sum(w); w /= s; // The algorithm is fairly simple-minded and is O(N^2), so be careful: we // pick the first element according to a multinomial distribution given by // the weights. Then we set the probability of that element to 0 and // renormalize the weights. We pick the second element according to the // new distribution, and so forth. for (int i=0 ; i<number_to_keep ; ++i) { int j = this->multinomial_sample(w); r[i] = vec[j]; // Set its weight to zero and renormalize. To keep numerical // precision, every 20 time-steps we do a full renormalization (and not // only an incremental one). if (i % 20 == 0) { w[j] = 0; real s = sum(w); w /= s; } else { real old_w = w[j]; w[j] = 0; w /= (1. - old_w); // New sum of weights is 1 minus what we took out } } return r; }
Reimplemented from PLearn::Object.
boost::exponential_distribution* PLearn::PRandom::exponential_distribution [protected] |
The underlying Boost distribution for exponential sampling.
Definition at line 82 of file PRandom.h.
Referenced by ensure_exponential_distribution(), exp_sample(), get_exponential_distribution(), operator=(), PRandom(), and ~PRandom().
int32_t PLearn::PRandom::fixed_seed [protected] |
Definition at line 97 of file PRandom.h.
Referenced by build_(), declareOptions(), get_fixed_seed(), manual_seed(), and operator=().
boost::normal_distribution* PLearn::PRandom::normal_distribution [protected] |
The underlying Boost distribution for normal sampling.
Definition at line 85 of file PRandom.h.
Referenced by ensure_normal_distribution(), gaussian_01(), get_normal_distribution(), operator=(), PRandom(), and ~PRandom().
boost::mt19937 PLearn::PRandom::rgen [protected] |
The underlying Boost random number generator used.
Definition at line 79 of file PRandom.h.
Referenced by ensure_uniform_01(), get_rgen(), manual_seed_(), operator=(), and split().
int32_t PLearn::PRandom::seed_ |
Definition at line 105 of file PRandom.h.
Referenced by build_(), declareOptions(), get_seed(), manual_seed(), and operator=().
uint32_t PLearn::PRandom::the_seed [protected] |
The actual seed used by the random number generator.
Definition at line 91 of file PRandom.h.
Referenced by get_the_seed(), manual_seed_(), and operator=().
boost::uniform_01<boost::mt19937>* PLearn::PRandom::uniform_01 [protected] |
The underlying Boost distribution for uniform sampling.
Definition at line 88 of file PRandom.h.
Referenced by ensure_uniform_01(), exp_sample(), gaussian_01(), get_uniform_01(), manual_seed_(), operator=(), PRandom(), uniform_sample(), and ~PRandom().