lemon/random.h
changeset 784 1a7fe3bef514
parent 559 c5fd2d996909
     1.1 --- a/lemon/random.h	Fri Oct 16 10:21:37 2009 +0200
     1.2 +++ b/lemon/random.h	Thu Nov 05 15:50:01 2009 +0100
     1.3 @@ -2,7 +2,7 @@
     1.4   *
     1.5   * This file is a part of LEMON, a generic C++ optimization library.
     1.6   *
     1.7 - * Copyright (C) 2003-2008
     1.8 + * Copyright (C) 2003-2009
     1.9   * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10   * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11   *
    1.12 @@ -530,10 +530,6 @@
    1.13      ///
    1.14      /// @{
    1.15  
    1.16 -    ///\name Initialization
    1.17 -    ///
    1.18 -    /// @{
    1.19 -
    1.20      /// \brief Default constructor
    1.21      ///
    1.22      /// Constructor with constant seeding.
    1.23 @@ -607,7 +603,7 @@
    1.24      /// By default, this function calls the \c seedFromFile() member
    1.25      /// function with the <tt>/dev/urandom</tt> file. If it does not success,
    1.26      /// it uses the \c seedFromTime().
    1.27 -    /// \return Currently always true.
    1.28 +    /// \return Currently always \c true.
    1.29      bool seed() {
    1.30  #ifndef WIN32
    1.31        if (seedFromFile("/dev/urandom", 0)) return true;
    1.32 @@ -628,7 +624,7 @@
    1.33      /// entropy).
    1.34      /// \param file The source file
    1.35      /// \param offset The offset, from the file read.
    1.36 -    /// \return True when the seeding successes.
    1.37 +    /// \return \c true when the seeding successes.
    1.38  #ifndef WIN32
    1.39      bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0)
    1.40  #else
    1.41 @@ -649,7 +645,7 @@
    1.42      /// Seding from process id and time. This function uses the
    1.43      /// current process id and the current time for initialize the
    1.44      /// random sequence.
    1.45 -    /// \return Currently always true.
    1.46 +    /// \return Currently always \c true.
    1.47      bool seedFromTime() {
    1.48  #ifndef WIN32
    1.49        timeval tv;
    1.50 @@ -663,7 +659,7 @@
    1.51  
    1.52      /// @}
    1.53  
    1.54 -    ///\name Uniform distributions
    1.55 +    ///\name Uniform Distributions
    1.56      ///
    1.57      /// @{
    1.58  
    1.59 @@ -680,12 +676,6 @@
    1.60        return real<double>();
    1.61      }
    1.62  
    1.63 -    /// @}
    1.64 -
    1.65 -    ///\name Uniform distributions
    1.66 -    ///
    1.67 -    /// @{
    1.68 -
    1.69      /// \brief Returns a random real number from the range [0, 1)
    1.70      ///
    1.71      /// It returns a random double from the range [0, 1).
    1.72 @@ -741,8 +731,6 @@
    1.73        return _random_bits::IntConversion<Number, Word>::convert(core);
    1.74      }
    1.75  
    1.76 -    /// @}
    1.77 -
    1.78      unsigned int uinteger() {
    1.79        return uinteger<unsigned int>();
    1.80      }
    1.81 @@ -774,21 +762,20 @@
    1.82  
    1.83      /// @}
    1.84  
    1.85 -    ///\name Non-uniform distributions
    1.86 +    ///\name Non-uniform Distributions
    1.87      ///
    1.88 -
    1.89      ///@{
    1.90  
    1.91 -    /// \brief Returns a random bool
    1.92 +    /// \brief Returns a random bool with given probability of true result.
    1.93      ///
    1.94      /// It returns a random bool with given probability of true result.
    1.95      bool boolean(double p) {
    1.96        return operator()() < p;
    1.97      }
    1.98  
    1.99 -    /// Standard Gauss distribution
   1.100 +    /// Standard normal (Gauss) distribution
   1.101  
   1.102 -    /// Standard Gauss distribution.
   1.103 +    /// Standard normal (Gauss) distribution.
   1.104      /// \note The Cartesian form of the Box-Muller
   1.105      /// transformation is used to generate a random normal distribution.
   1.106      double gauss()
   1.107 @@ -801,15 +788,55 @@
   1.108        } while(S>=1);
   1.109        return std::sqrt(-2*std::log(S)/S)*V1;
   1.110      }
   1.111 -    /// Gauss distribution with given mean and standard deviation
   1.112 +    /// Normal (Gauss) distribution with given mean and standard deviation
   1.113  
   1.114 -    /// Gauss distribution with given mean and standard deviation.
   1.115 +    /// Normal (Gauss) distribution with given mean and standard deviation.
   1.116      /// \sa gauss()
   1.117      double gauss(double mean,double std_dev)
   1.118      {
   1.119        return gauss()*std_dev+mean;
   1.120      }
   1.121  
   1.122 +    /// Lognormal distribution
   1.123 +
   1.124 +    /// Lognormal distribution. The parameters are the mean and the standard
   1.125 +    /// deviation of <tt>exp(X)</tt>.
   1.126 +    ///
   1.127 +    double lognormal(double n_mean,double n_std_dev)
   1.128 +    {
   1.129 +      return std::exp(gauss(n_mean,n_std_dev));
   1.130 +    }
   1.131 +    /// Lognormal distribution
   1.132 +
   1.133 +    /// Lognormal distribution. The parameter is an <tt>std::pair</tt> of
   1.134 +    /// the mean and the standard deviation of <tt>exp(X)</tt>.
   1.135 +    ///
   1.136 +    double lognormal(const std::pair<double,double> &params)
   1.137 +    {
   1.138 +      return std::exp(gauss(params.first,params.second));
   1.139 +    }
   1.140 +    /// Compute the lognormal parameters from mean and standard deviation
   1.141 +
   1.142 +    /// This function computes the lognormal parameters from mean and
   1.143 +    /// standard deviation. The return value can direcly be passed to
   1.144 +    /// lognormal().
   1.145 +    std::pair<double,double> lognormalParamsFromMD(double mean,
   1.146 +                                                   double std_dev)
   1.147 +    {
   1.148 +      double fr=std_dev/mean;
   1.149 +      fr*=fr;
   1.150 +      double lg=std::log(1+fr);
   1.151 +      return std::pair<double,double>(std::log(mean)-lg/2.0,std::sqrt(lg));
   1.152 +    }
   1.153 +    /// Lognormal distribution with given mean and standard deviation
   1.154 +
   1.155 +    /// Lognormal distribution with given mean and standard deviation.
   1.156 +    ///
   1.157 +    double lognormalMD(double mean,double std_dev)
   1.158 +    {
   1.159 +      return lognormal(lognormalParamsFromMD(mean,std_dev));
   1.160 +    }
   1.161 +
   1.162      /// Exponential distribution with given mean
   1.163  
   1.164      /// This function generates an exponential distribution random number
   1.165 @@ -911,9 +938,8 @@
   1.166  
   1.167      ///@}
   1.168  
   1.169 -    ///\name Two dimensional distributions
   1.170 +    ///\name Two Dimensional Distributions
   1.171      ///
   1.172 -
   1.173      ///@{
   1.174  
   1.175      /// Uniform distribution on the full unit circle
   1.176 @@ -930,7 +956,7 @@
   1.177        } while(V1*V1+V2*V2>=1);
   1.178        return dim2::Point<double>(V1,V2);
   1.179      }
   1.180 -    /// A kind of two dimensional Gauss distribution
   1.181 +    /// A kind of two dimensional normal (Gauss) distribution
   1.182  
   1.183      /// This function provides a turning symmetric two-dimensional distribution.
   1.184      /// Both coordinates are of standard normal distribution, but they are not