lemon/random.h
changeset 364 b4a01426c0d9
parent 339 2593e163e407
child 378 80ec623f529f
equal deleted inserted replaced
17:386058cfed26 18:8034abbbcfca
   538 
   538 
   539     ///\name Initialization
   539     ///\name Initialization
   540     ///
   540     ///
   541     /// @{
   541     /// @{
   542 
   542 
   543     ///\name Initialization
       
   544     ///
       
   545     /// @{
       
   546 
       
   547     /// \brief Default constructor
   543     /// \brief Default constructor
   548     ///
   544     ///
   549     /// Constructor with constant seeding.
   545     /// Constructor with constant seeding.
   550     Random() { core.initState(); }
   546     Random() { core.initState(); }
   551 
   547 
   706     template <typename Number>
   702     template <typename Number>
   707     Number real(Number a, Number b) {
   703     Number real(Number a, Number b) {
   708       return real<Number>() * (b - a) + a;
   704       return real<Number>() * (b - a) + a;
   709     }
   705     }
   710 
   706 
   711     /// @}
       
   712 
       
   713     ///\name Uniform distributions
       
   714     ///
       
   715     /// @{
       
   716 
       
   717     /// \brief Returns a random real number from the range [0, 1)
   707     /// \brief Returns a random real number from the range [0, 1)
   718     ///
   708     ///
   719     /// It returns a random double from the range [0, 1).
   709     /// It returns a random double from the range [0, 1).
   720     double operator()() {
   710     double operator()() {
   721       return real<double>();
   711       return real<double>();
   768     /// type of this function is <tt>unsigned int</tt>.
   758     /// type of this function is <tt>unsigned int</tt>.
   769     template <typename Number>
   759     template <typename Number>
   770     Number uinteger() {
   760     Number uinteger() {
   771       return _random_bits::IntConversion<Number, Word>::convert(core);
   761       return _random_bits::IntConversion<Number, Word>::convert(core);
   772     }
   762     }
   773 
       
   774     /// @}
       
   775 
   763 
   776     unsigned int uinteger() {
   764     unsigned int uinteger() {
   777       return uinteger<unsigned int>();
   765       return uinteger<unsigned int>();
   778     }
   766     }
   779 
   767 
   804 
   792 
   805     /// @}
   793     /// @}
   806 
   794 
   807     ///\name Non-uniform distributions
   795     ///\name Non-uniform distributions
   808     ///
   796     ///
   809 
       
   810     ///@{
   797     ///@{
   811 
   798 
   812     /// \brief Returns a random bool
   799     /// \brief Returns a random bool with given probability of true result.
   813     ///
   800     ///
   814     /// It returns a random bool with given probability of true result.
   801     /// It returns a random bool with given probability of true result.
   815     bool boolean(double p) {
   802     bool boolean(double p) {
   816       return operator()() < p;
   803       return operator()() < p;
   817     }
   804     }
   818 
   805 
   819     /// Standard Gauss distribution
   806     /// Standard normal (Gauss) distribution
   820 
   807 
   821     /// Standard Gauss distribution.
   808     /// Standard normal (Gauss) distribution.
   822     /// \note The Cartesian form of the Box-Muller
   809     /// \note The Cartesian form of the Box-Muller
   823     /// transformation is used to generate a random normal distribution.
   810     /// transformation is used to generate a random normal distribution.
   824     double gauss()
   811     double gauss()
   825     {
   812     {
   826       double V1,V2,S;
   813       double V1,V2,S;
   829         V2=2*real<double>()-1;
   816         V2=2*real<double>()-1;
   830         S=V1*V1+V2*V2;
   817         S=V1*V1+V2*V2;
   831       } while(S>=1);
   818       } while(S>=1);
   832       return std::sqrt(-2*std::log(S)/S)*V1;
   819       return std::sqrt(-2*std::log(S)/S)*V1;
   833     }
   820     }
   834     /// Gauss distribution with given mean and standard deviation
   821     /// Normal (Gauss) distribution with given mean and standard deviation
   835 
   822 
   836     /// Gauss distribution with given mean and standard deviation.
   823     /// Normal (Gauss) distribution with given mean and standard deviation.
   837     /// \sa gauss()
   824     /// \sa gauss()
   838     double gauss(double mean,double std_dev)
   825     double gauss(double mean,double std_dev)
   839     {
   826     {
   840       return gauss()*std_dev+mean;
   827       return gauss()*std_dev+mean;
   841     }
   828     }
   862 
   849 
   863     /// This function computes the lognormal parameters from mean and
   850     /// This function computes the lognormal parameters from mean and
   864     /// standard deviation. The return value can direcly be passed to
   851     /// standard deviation. The return value can direcly be passed to
   865     /// lognormal().
   852     /// lognormal().
   866     std::pair<double,double> lognormalParamsFromMD(double mean,
   853     std::pair<double,double> lognormalParamsFromMD(double mean,
   867 						   double std_dev)
   854                                                    double std_dev)
   868     {
   855     {
   869       double fr=std_dev/mean;
   856       double fr=std_dev/mean;
   870       fr*=fr;
   857       fr*=fr;
   871       double lg=std::log(1+fr);
   858       double lg=std::log(1+fr);
   872       return std::pair<double,double>(std::log(mean)-lg/2.0,std::sqrt(lg));
   859       return std::pair<double,double>(std::log(mean)-lg/2.0,std::sqrt(lg));
   873     }
   860     }
   874     /// Lognormal distribution with given mean and standard deviation
   861     /// Lognormal distribution with given mean and standard deviation
   875     
   862 
   876     /// Lognormal distribution with given mean and standard deviation.
   863     /// Lognormal distribution with given mean and standard deviation.
   877     ///
   864     ///
   878     double lognormalMD(double mean,double std_dev)
   865     double lognormalMD(double mean,double std_dev)
   879     {
   866     {
   880       return lognormal(lognormalParamsFromMD(mean,std_dev));
   867       return lognormal(lognormalParamsFromMD(mean,std_dev));
   881     }
   868     }
   882     
   869 
   883     /// Exponential distribution with given mean
   870     /// Exponential distribution with given mean
   884 
   871 
   885     /// This function generates an exponential distribution random number
   872     /// This function generates an exponential distribution random number
   886     /// with mean <tt>1/lambda</tt>.
   873     /// with mean <tt>1/lambda</tt>.
   887     ///
   874     ///
   981 
   968 
   982     ///@}
   969     ///@}
   983 
   970 
   984     ///\name Two dimensional distributions
   971     ///\name Two dimensional distributions
   985     ///
   972     ///
   986 
       
   987     ///@{
   973     ///@{
   988 
   974 
   989     /// Uniform distribution on the full unit circle
   975     /// Uniform distribution on the full unit circle
   990 
   976 
   991     /// Uniform distribution on the full unit circle.
   977     /// Uniform distribution on the full unit circle.
   998         V2=2*real<double>()-1;
   984         V2=2*real<double>()-1;
   999 
   985 
  1000       } while(V1*V1+V2*V2>=1);
   986       } while(V1*V1+V2*V2>=1);
  1001       return dim2::Point<double>(V1,V2);
   987       return dim2::Point<double>(V1,V2);
  1002     }
   988     }
  1003     /// A kind of two dimensional Gauss distribution
   989     /// A kind of two dimensional normal (Gauss) distribution
  1004 
   990 
  1005     /// This function provides a turning symmetric two-dimensional distribution.
   991     /// This function provides a turning symmetric two-dimensional distribution.
  1006     /// Both coordinates are of standard normal distribution, but they are not
   992     /// Both coordinates are of standard normal distribution, but they are not
  1007     /// independent.
   993     /// independent.
  1008     ///
   994     ///