lemon/random.h
changeset 2363 2aabce558574
parent 2355 ac0d843b8873
child 2372 7fcc0179fb21
equal deleted inserted replaced
6:74484647d5e4 7:8366fe8ba676
   622     /// It returns a random bool
   622     /// It returns a random bool
   623     bool boolean() {
   623     bool boolean() {
   624       return _random_bits::BoolConversion<Word>::convert(core);
   624       return _random_bits::BoolConversion<Word>::convert(core);
   625     }
   625     }
   626 
   626 
       
   627     ///\name Nonuniform distributions
       
   628     ///
       
   629     
       
   630     ///@{
       
   631     
   627     /// \brief Returns a random bool
   632     /// \brief Returns a random bool
   628     ///
   633     ///
   629     /// It returns a random bool with given probability of true result
   634     /// It returns a random bool with given probability of true result
   630     bool boolean(double p) {
   635     bool boolean(double p) {
   631       return operator()() < p;
   636       return operator()() < p;
   632     }
   637     }
   633 
   638 
   634     /// Standard Gauss distribution
   639     /// Standard Gauss distribution
   635 
   640 
   636     /// Standard Gauss distribution.
   641     /// Standard Gauss distribution.
   637     /// \todo Currently it uses the so-calles "polar technique" to generate
   642     /// \note The Cartesian form of the Box-Muller
   638     /// random distribution.
   643     /// transformation is used to generate a random normal distribution.
   639     /// Probably, the "Ziggurat" method should rather be used.
   644     /// \todo Consider using the "ziggurat" method instead.
   640     double gauss() 
   645     double gauss() 
   641     {
   646     {
   642       double V1,V2,S;
   647       double V1,V2,S;
   643       do {
   648       do {
   644 	V1=2*real<double>()-1;
   649 	V1=2*real<double>()-1;
   645 	V2=2*real<double>()-1;
   650 	V2=2*real<double>()-1;
   646 	S=V1*V1+V2*V2;
   651 	S=V1*V1+V2*V2;
   647       } while(S>=1);
   652       } while(S>=1);
   648       return std::sqrt(-2*std::log(S)/S)*V1;
   653       return std::sqrt(-2*std::log(S)/S)*V1;
   649     }
   654     }
   650     /// Gauss distribution with given variance and mean 0
   655     /// Gauss distribution with given standard deviation and mean 0
   651     double gauss(double var) 
   656 
       
   657     /// \sa gauss()
       
   658     ///
       
   659     double gauss(double std_dev) 
   652     {
   660     {
   653       return gauss()*var;
   661       return gauss()*std_dev;
   654     }
   662     }
   655     /// Gauss distribution with given variance and mean
   663     /// Gauss distribution with given mean and standard deviation
   656     double gauss(double var,double mean)
   664 
       
   665     /// \sa gauss()
       
   666     ///
       
   667     double gauss(double mean,double std_dev)
   657     {
   668     {
   658       return gauss()*var+mean;
   669       return gauss()*std_dev+mean;
   659     }
   670     }
   660 
   671 
   661     double exponential(double lambda)
   672     /// Exponential distribution with given mean
       
   673 
       
   674     /// This function generates an exponential distribution random number
       
   675     /// with mean <tt>1/lambda</tt>.
       
   676     ///
       
   677     double exponential(double lambda=1.0)
   662     {
   678     {
   663       return -log(real<double>())/lambda;
   679       return -log(real<double>())/lambda;
   664     }
   680     }
       
   681 
       
   682     ///@}
   665     
   683     
   666   };
   684   };
   667 
   685 
   668 
   686 
   669   extern Random rnd;
   687   extern Random rnd;