lemon/random.h
changeset 2490 31a93dd6f714
parent 2391 14a343be7a5a
child 2545 2bed3e806e1e
equal deleted inserted replaced
13:58a768564611 14:a34f0a0b8a9c
   746     double exponential(double lambda=1.0)
   746     double exponential(double lambda=1.0)
   747     {
   747     {
   748       return -std::log(real<double>())/lambda;
   748       return -std::log(real<double>())/lambda;
   749     }
   749     }
   750 
   750 
       
   751     double gamma(int k) 
       
   752     {
       
   753       double s = 0;
       
   754       for(int i=0;i<k;i++) s-=std::log(1.0-real<double>());
       
   755       return s;
       
   756     }
       
   757     
       
   758     /// Gamma distribution with given shape and scale parameter
       
   759 
       
   760     /// This function generates a gamma distribution random number.
       
   761     /// 
       
   762     ///\param k shape parameter (<tt>k>0</tt>)
       
   763     ///\param theta scale parameter
       
   764     ///
       
   765     double gamma(double k,double theta=1.0)
       
   766     {
       
   767       double xi,nu;
       
   768       const double delta = k-std::floor(k);
       
   769       const double v0=M_E/(M_E-delta);
       
   770       do {
       
   771 	double V0=1.0-real<double>();
       
   772 	double V1=1.0-real<double>();
       
   773 	double V2=1.0-real<double>();
       
   774 	if(V2<=v0) 
       
   775 	  {
       
   776 	    xi=std::pow(V1,1.0/delta);
       
   777 	    nu=V0*std::pow(xi,delta-1.0);
       
   778 	  }
       
   779 	else 
       
   780 	  {
       
   781 	    xi=1.0-std::log(V1);
       
   782 	    nu=V0*std::exp(-xi);
       
   783 	  }
       
   784       } while(nu>std::pow(xi,delta-1.0)*std::exp(-xi));
       
   785       return theta*(xi-gamma(int(std::floor(k))));
       
   786     }
       
   787     
       
   788       
   751     ///@}
   789     ///@}
   752     
   790     
   753     ///\name Two dimensional distributions
   791     ///\name Two dimensional distributions
   754     ///
   792     ///
   755 
   793