lemon/random.h
changeset 2355 ac0d843b8873
parent 2285 8c5c4b5ae31c
child 2356 57c316cb868b
equal deleted inserted replaced
5:5f956d665e24 6:74484647d5e4
    22 #include <algorithm>
    22 #include <algorithm>
    23 #include <iterator>
    23 #include <iterator>
    24 #include <vector>
    24 #include <vector>
    25 
    25 
    26 #include <ctime>
    26 #include <ctime>
    27 #include <cmath>
       
    28 #include <cmath>
    27 #include <cmath>
    29 
    28 
    30 ///\ingroup misc
    29 ///\ingroup misc
    31 ///\file
    30 ///\file
    32 ///\brief Mersenne Twister random number generator
    31 ///\brief Mersenne Twister random number generator
   629     ///
   628     ///
   630     /// It returns a random bool with given probability of true result
   629     /// It returns a random bool with given probability of true result
   631     bool boolean(double p) {
   630     bool boolean(double p) {
   632       return operator()() < p;
   631       return operator()() < p;
   633     }
   632     }
       
   633 
       
   634     /// Standard Gauss distribution
       
   635 
       
   636     /// Standard Gauss distribution.
       
   637     /// \todo Currently it uses the so-calles "polar technique" to generate
       
   638     /// random distribution.
       
   639     /// Probably, the "Ziggurat" method should rather be used.
       
   640     double gauss() 
       
   641     {
       
   642       double V1,V2,S;
       
   643       do {
       
   644 	V1=2*real<double>()-1;
       
   645 	V2=2*real<double>()-1;
       
   646 	S=V1*V1+V2*V2;
       
   647       } while(S>=1);
       
   648       return std::sqrt(-2*std::log(S)/S)*V1;
       
   649     }
       
   650     /// Gauss distribution with given variance and mean 0
       
   651     double gauss(double var) 
       
   652     {
       
   653       return gauss()*var;
       
   654     }
       
   655     /// Gauss distribution with given variance and mean
       
   656     double gauss(double var,double mean)
       
   657     {
       
   658       return gauss()*var+mean;
       
   659     }
       
   660 
       
   661     double exponential(double lambda)
       
   662     {
       
   663       return -log(real<double>())/lambda;
       
   664     }
   634     
   665     
   635   };
   666   };
   636 
   667 
   637 
   668 
   638   extern Random rnd;
   669   extern Random rnd;