diff -r c6acc34f98dc -r 1a7fe3bef514 lemon/random.h --- a/lemon/random.h Fri Oct 16 10:21:37 2009 +0200 +++ b/lemon/random.h Thu Nov 05 15:50:01 2009 +0100 @@ -2,7 +2,7 @@ * * This file is a part of LEMON, a generic C++ optimization library. * - * Copyright (C) 2003-2008 + * Copyright (C) 2003-2009 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Research Group on Combinatorial Optimization, EGRES). * @@ -530,10 +530,6 @@ /// /// @{ - ///\name Initialization - /// - /// @{ - /// \brief Default constructor /// /// Constructor with constant seeding. @@ -607,7 +603,7 @@ /// By default, this function calls the \c seedFromFile() member /// function with the /dev/urandom file. If it does not success, /// it uses the \c seedFromTime(). - /// \return Currently always true. + /// \return Currently always \c true. bool seed() { #ifndef WIN32 if (seedFromFile("/dev/urandom", 0)) return true; @@ -628,7 +624,7 @@ /// entropy). /// \param file The source file /// \param offset The offset, from the file read. - /// \return True when the seeding successes. + /// \return \c true when the seeding successes. #ifndef WIN32 bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0) #else @@ -649,7 +645,7 @@ /// Seding from process id and time. This function uses the /// current process id and the current time for initialize the /// random sequence. - /// \return Currently always true. + /// \return Currently always \c true. bool seedFromTime() { #ifndef WIN32 timeval tv; @@ -663,7 +659,7 @@ /// @} - ///\name Uniform distributions + ///\name Uniform Distributions /// /// @{ @@ -680,12 +676,6 @@ return real(); } - /// @} - - ///\name Uniform distributions - /// - /// @{ - /// \brief Returns a random real number from the range [0, 1) /// /// It returns a random double from the range [0, 1). @@ -741,8 +731,6 @@ return _random_bits::IntConversion::convert(core); } - /// @} - unsigned int uinteger() { return uinteger(); } @@ -774,21 +762,20 @@ /// @} - ///\name Non-uniform distributions + ///\name Non-uniform Distributions /// - ///@{ - /// \brief Returns a random bool + /// \brief Returns a random bool with given probability of true result. /// /// It returns a random bool with given probability of true result. bool boolean(double p) { return operator()() < p; } - /// Standard Gauss distribution + /// Standard normal (Gauss) distribution - /// Standard Gauss distribution. + /// Standard normal (Gauss) distribution. /// \note The Cartesian form of the Box-Muller /// transformation is used to generate a random normal distribution. double gauss() @@ -801,15 +788,55 @@ } while(S>=1); return std::sqrt(-2*std::log(S)/S)*V1; } - /// Gauss distribution with given mean and standard deviation + /// Normal (Gauss) distribution with given mean and standard deviation - /// Gauss distribution with given mean and standard deviation. + /// Normal (Gauss) distribution with given mean and standard deviation. /// \sa gauss() double gauss(double mean,double std_dev) { return gauss()*std_dev+mean; } + /// Lognormal distribution + + /// Lognormal distribution. The parameters are the mean and the standard + /// deviation of exp(X). + /// + double lognormal(double n_mean,double n_std_dev) + { + return std::exp(gauss(n_mean,n_std_dev)); + } + /// Lognormal distribution + + /// Lognormal distribution. The parameter is an std::pair of + /// the mean and the standard deviation of exp(X). + /// + double lognormal(const std::pair ¶ms) + { + return std::exp(gauss(params.first,params.second)); + } + /// Compute the lognormal parameters from mean and standard deviation + + /// This function computes the lognormal parameters from mean and + /// standard deviation. The return value can direcly be passed to + /// lognormal(). + std::pair lognormalParamsFromMD(double mean, + double std_dev) + { + double fr=std_dev/mean; + fr*=fr; + double lg=std::log(1+fr); + return std::pair(std::log(mean)-lg/2.0,std::sqrt(lg)); + } + /// Lognormal distribution with given mean and standard deviation + + /// Lognormal distribution with given mean and standard deviation. + /// + double lognormalMD(double mean,double std_dev) + { + return lognormal(lognormalParamsFromMD(mean,std_dev)); + } + /// Exponential distribution with given mean /// This function generates an exponential distribution random number @@ -911,9 +938,8 @@ ///@} - ///\name Two dimensional distributions + ///\name Two Dimensional Distributions /// - ///@{ /// Uniform distribution on the full unit circle @@ -930,7 +956,7 @@ } while(V1*V1+V2*V2>=1); return dim2::Point(V1,V2); } - /// A kind of two dimensional Gauss distribution + /// A kind of two dimensional normal (Gauss) distribution /// This function provides a turning symmetric two-dimensional distribution. /// Both coordinates are of standard normal distribution, but they are not