Some two dimensional random distribution added.
authoralpar
Tue, 20 Feb 2007 13:01:22 +0000
changeset 2374b59a17034ffa
parent 2373 134639e6ea45
child 2375 e30a0fdad0d7
Some two dimensional random distribution added.
They should be revised.
lemon/random.h
     1.1 --- a/lemon/random.h	Tue Feb 20 12:55:37 2007 +0000
     1.2 +++ b/lemon/random.h	Tue Feb 20 13:01:22 2007 +0000
     1.3 @@ -69,6 +69,7 @@
     1.4  #include <ctime>
     1.5  #include <cmath>
     1.6  
     1.7 +#include <lemon/dim2.h>
     1.8  ///\ingroup misc
     1.9  ///\file
    1.10  ///\brief Mersenne Twister random number generator
    1.11 @@ -742,11 +743,66 @@
    1.12      ///
    1.13      double exponential(double lambda=1.0)
    1.14      {
    1.15 -      return -log(real<double>())/lambda;
    1.16 +      return -std::log(real<double>())/lambda;
    1.17      }
    1.18  
    1.19      ///@}
    1.20      
    1.21 +    ///\name Two dimensional distributions
    1.22 +    ///
    1.23 +
    1.24 +    ///@{
    1.25 +    
    1.26 +    /// Uniform distribution on the full unit circle.
    1.27 +    dim2::Point<double> ball2() 
    1.28 +    {
    1.29 +      double V1,V2;
    1.30 +      do {
    1.31 +	V1=2*real<double>()-1;
    1.32 +	V2=2*real<double>()-1;
    1.33 +	
    1.34 +      } while(V1*V1+V2*V2>=1);
    1.35 +      return dim2::Point<double>(V1,V2);
    1.36 +    }
    1.37 +    /// A kind of two dimensional Gauss distribution
    1.38 +
    1.39 +    /// This function provides a turning symmetric two-dimensional distribution.
    1.40 +    /// Both coordinates are of standard normal distribution, but they are not
    1.41 +    /// independent.
    1.42 +    ///
    1.43 +    /// \note The coordinates are the two random variables provided by
    1.44 +    /// the Box-Muller method.
    1.45 +    dim2::Point<double> gauss2()
    1.46 +    {
    1.47 +      double V1,V2,S;
    1.48 +      do {
    1.49 +	V1=2*real<double>()-1;
    1.50 +	V2=2*real<double>()-1;
    1.51 +	S=V1*V1+V2*V2;
    1.52 +      } while(S>=1);
    1.53 +      double W=std::sqrt(-2*std::log(S)/S);
    1.54 +      return dim2::Point<double>(W*V1,W*V2);
    1.55 +    }
    1.56 +    /// A kind of two dimensional exponential distribution
    1.57 +
    1.58 +    /// This function provides a turning symmetric two-dimensional distribution.
    1.59 +    /// The x-coordinate is of conditionally exponential distribution
    1.60 +    /// with the condition that x is positive and y=0. If x is negative and 
    1.61 +    /// y=0 then, -x is of exponential distribution. The same is true for the
    1.62 +    /// y-coordinate.
    1.63 +    dim2::Point<double> exponential2() 
    1.64 +    {
    1.65 +      double V1,V2,S;
    1.66 +      do {
    1.67 +	V1=2*real<double>()-1;
    1.68 +	V2=2*real<double>()-1;
    1.69 +	S=V1*V1+V2*V2;
    1.70 +      } while(S>=1);
    1.71 +      double W=-std::log(S)/S;
    1.72 +      return dim2::Point<double>(W*V1,W*V2);
    1.73 +    }
    1.74 +
    1.75 +    ///@}    
    1.76    };
    1.77  
    1.78