Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# rnd_generators.h

    00001 /* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003   -----------------------------------------------------------------------------
    00004   rnd_generators.h
    00005     Some utility functors for generating random generators:
    00006         uniform_generator : generates uniform floats or doubles
    00007         random_generator  : generates unsigneds, ints etc.
    00008         normal_generator  : normally distributed floats or doubles
    00009
    00010  (c) Maarten Keijzer (mak@dhi.dk) and GeNeura Team, 1999, 2000
    00011
    00012     This library is free software; you can redistribute it and/or
    00013     modify it under the terms of the GNU Lesser General Public
    00014     License as published by the Free Software Foundation; either
    00015     version 2 of the License, or (at your option) any later version.
    00016
    00017     This library is distributed in the hope that it will be useful,
    00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00020     Lesser General Public License for more details.
    00021
    00022     You should have received a copy of the GNU Lesser General Public
    00023     License along with this library; if not, write to the Free Software
    00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00025
    00026     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
    00027  */
    00028
    00029 //-----------------------------------------------------------------------------
    00030
    00031 #ifndef eoRND_GENERATORS_H
    00032 #define eoRND_GENERATORS_H
    00033
    00034 #include "eoRNG.h"
    00035 #include <stdexcept>
    00036
    00044 template <class T = double> class uniform_generator
    00045 {
    00046   // added new ctor with 2 params, and modified the data to minim and range
    00047   // (was maxim only). MS 3/11/2000
    00048   public :
    00049     uniform_generator(T _max = T(1.0), eoRng& _rng = rng) :
    00050       minim(T(0.0)), range(_max), uniform(_rng) {}
    00051     uniform_generator(T _min, T _max, eoRng& _rng = rng) :
    00052       minim(_min), range(_max-_min), uniform(_rng)
    00053   {
    00054     if (_min>_max)
    00055       throw std::logic_error("Min is greater than Max in uniform_generator");
    00056   }
    00057
    00058   T operator()(void) { return minim+static_cast<T>(uniform.uniform(range)); }
    00059
    00060 private :
    00061   T minim;
    00062   T range;
    00063   eoRng& uniform;
    00064 };
    00065
    00070 class boolean_generator
    00071 {
    00072   public :
    00073     boolean_generator(float _bias = 0.5, eoRng& _rng = rng) : bias(_bias), gen(_rng) {}
    00074
    00075   bool operator()(void) { return gen.flip(bias); }
    00076   private :
    00077   float bias;
    00078   eoRng& gen;
    00079 };
    00080
    00087 template <class T = uint32_t> class random_generator
    00088 {
    00089   public :
    00090   // added new ctor with 2 params, and modified the data to minim and range
    00091   // (was maxim only). MS 3/11/2000
    00092     random_generator(T _max, eoRng& _rng = rng) :
    00093       minim(T(0.0)), range(_max), random(_rng) {}
    00094     random_generator(T _min, T _max, eoRng& _rng = rng) :
    00095       minim(_min), range(_max-_min), random(_rng)
    00096   {
    00097     if (_min>_max)
    00098       throw std::logic_error("Min is greater than Max in random_generator");
    00099   }
    00100
    00101   T operator()(void) { return (T) (minim + random.random(range)); }
    00102
    00103 private :
    00104   T minim;
    00105   T range;
    00106   eoRng& random;
    00107 };
    00108
    00110 template <>
    00111 inline bool random_generator<bool>::operator()(void)
    00112 {
    00113     return random.flip(0.5);
    00114 }
    00115
    00121 template <class T = uint32_t> class UF_random_generator
    00122 {
    00123   public :
    00124     UF_random_generator(eoRng& _rng = rng) :
    00125       random(_rng) {}
    00126
    00127   T operator()(T _t) { return (T) (random.random(_t)); }
    00128
    00129 private :
    00130   eoRng& random;
    00131 };
    00132
    00133
    00139 template <class T = double> class normal_generator
    00140 {
    00141   public :
    00142     normal_generator(T _stdev = T(1.0), eoRng& _rng = rng) : stdev(_stdev), normal(_rng) {}
    00143
    00144   T operator()(void) { return (T) normal.normal(stdev); }
    00145
    00146   private :
    00147     T stdev;
    00148   eoRng& normal;
    00149 };
    00150
    00156 template <class T = double> class negexp_generator
    00157 {
    00158   public :
    00159     negexp_generator(T _mean = 1.0, eoRng& _rng = rng) : mean(_mean), negexp(_rng) {}
    00160
    00161   T operator()(void) { return (T) negexp.negexp(mean); }
    00162
    00163   private :
    00164     T mean;
    00165   eoRng& negexp;
    00166 };
    00167
    00168 #endif