lemon/random.h
changeset 2379 248152674a9e
parent 2372 7fcc0179fb21
child 2380 7b0558c52de3
equal deleted inserted replaced
8:0cd09c745503 9:0f57753dd885
    67 #include <vector>
    67 #include <vector>
    68 
    68 
    69 #include <ctime>
    69 #include <ctime>
    70 #include <cmath>
    70 #include <cmath>
    71 
    71 
       
    72 #include <lemon/dim2.h>
    72 ///\ingroup misc
    73 ///\ingroup misc
    73 ///\file
    74 ///\file
    74 ///\brief Mersenne Twister random number generator
    75 ///\brief Mersenne Twister random number generator
    75 ///
    76 ///
    76 ///\author Balazs Dezso
    77 ///\author Balazs Dezso
   740     /// This function generates an exponential distribution random number
   741     /// This function generates an exponential distribution random number
   741     /// with mean <tt>1/lambda</tt>.
   742     /// with mean <tt>1/lambda</tt>.
   742     ///
   743     ///
   743     double exponential(double lambda=1.0)
   744     double exponential(double lambda=1.0)
   744     {
   745     {
   745       return -log(real<double>())/lambda;
   746       return -std::log(real<double>())/lambda;
   746     }
   747     }
   747 
   748 
   748     ///@}
   749     ///@}
   749     
   750     
       
   751     ///\name Two dimensional distributions
       
   752     ///
       
   753 
       
   754     ///@{
       
   755     
       
   756     /// Uniform distribution on the full unit circle.
       
   757     dim2::Point<double> ball2() 
       
   758     {
       
   759       double V1,V2;
       
   760       do {
       
   761 	V1=2*real<double>()-1;
       
   762 	V2=2*real<double>()-1;
       
   763 	
       
   764       } while(V1*V1+V2*V2>=1);
       
   765       return dim2::Point<double>(V1,V2);
       
   766     }
       
   767     /// A kind of two dimensional Gauss distribution
       
   768 
       
   769     /// This function provides a turning symmetric two-dimensional distribution.
       
   770     /// Both coordinates are of standard normal distribution, but they are not
       
   771     /// independent.
       
   772     ///
       
   773     /// \note The coordinates are the two random variables provided by
       
   774     /// the Box-Muller method.
       
   775     dim2::Point<double> gauss2()
       
   776     {
       
   777       double V1,V2,S;
       
   778       do {
       
   779 	V1=2*real<double>()-1;
       
   780 	V2=2*real<double>()-1;
       
   781 	S=V1*V1+V2*V2;
       
   782       } while(S>=1);
       
   783       double W=std::sqrt(-2*std::log(S)/S);
       
   784       return dim2::Point<double>(W*V1,W*V2);
       
   785     }
       
   786     /// A kind of two dimensional exponential distribution
       
   787 
       
   788     /// This function provides a turning symmetric two-dimensional distribution.
       
   789     /// The x-coordinate is of conditionally exponential distribution
       
   790     /// with the condition that x is positive and y=0. If x is negative and 
       
   791     /// y=0 then, -x is of exponential distribution. The same is true for the
       
   792     /// y-coordinate.
       
   793     dim2::Point<double> exponential2() 
       
   794     {
       
   795       double V1,V2,S;
       
   796       do {
       
   797 	V1=2*real<double>()-1;
       
   798 	V2=2*real<double>()-1;
       
   799 	S=V1*V1+V2*V2;
       
   800       } while(S>=1);
       
   801       double W=-std::log(S)/S;
       
   802       return dim2::Point<double>(W*V1,W*V2);
       
   803     }
       
   804 
       
   805     ///@}    
   750   };
   806   };
   751 
   807 
   752 
   808 
   753   extern Random rnd;
   809   extern Random rnd;
   754 
   810