... | ... |
@@ -702,214 +702,214 @@ |
702 | 702 |
|
703 | 703 |
/// \brief Returns a random bool |
704 | 704 |
/// |
705 | 705 |
/// It returns a random bool. The generator holds a buffer for |
706 | 706 |
/// random bits. Every time when it become empty the generator makes |
707 | 707 |
/// a new random word and fill the buffer up. |
708 | 708 |
bool boolean() { |
709 | 709 |
return bool_producer.convert(core); |
710 | 710 |
} |
711 | 711 |
|
712 | 712 |
///\name Non-uniform distributions |
713 | 713 |
/// |
714 | 714 |
|
715 | 715 |
///@{ |
716 | 716 |
|
717 | 717 |
/// \brief Returns a random bool |
718 | 718 |
/// |
719 | 719 |
/// It returns a random bool with given probability of true result. |
720 | 720 |
bool boolean(double p) { |
721 | 721 |
return operator()() < p; |
722 | 722 |
} |
723 | 723 |
|
724 | 724 |
/// Standard Gauss distribution |
725 | 725 |
|
726 | 726 |
/// Standard Gauss distribution. |
727 | 727 |
/// \note The Cartesian form of the Box-Muller |
728 | 728 |
/// transformation is used to generate a random normal distribution. |
729 | 729 |
/// \todo Consider using the "ziggurat" method instead. |
730 | 730 |
double gauss() |
731 | 731 |
{ |
732 | 732 |
double V1,V2,S; |
733 | 733 |
do { |
734 | 734 |
V1=2*real<double>()-1; |
735 | 735 |
V2=2*real<double>()-1; |
736 | 736 |
S=V1*V1+V2*V2; |
737 | 737 |
} while(S>=1); |
738 | 738 |
return std::sqrt(-2*std::log(S)/S)*V1; |
739 | 739 |
} |
740 | 740 |
/// Gauss distribution with given mean and standard deviation |
741 | 741 |
|
742 | 742 |
/// Gauss distribution with given mean and standard deviation. |
743 | 743 |
/// \sa gauss() |
744 | 744 |
double gauss(double mean,double std_dev) |
745 | 745 |
{ |
746 | 746 |
return gauss()*std_dev+mean; |
747 | 747 |
} |
748 | 748 |
|
749 | 749 |
/// Exponential distribution with given mean |
750 | 750 |
|
751 | 751 |
/// This function generates an exponential distribution random number |
752 | 752 |
/// with mean <tt>1/lambda</tt>. |
753 | 753 |
/// |
754 | 754 |
double exponential(double lambda=1.0) |
755 | 755 |
{ |
756 | 756 |
return -std::log(1.0-real<double>())/lambda; |
757 | 757 |
} |
758 | 758 |
|
759 | 759 |
/// Gamma distribution with given integer shape |
760 | 760 |
|
761 | 761 |
/// This function generates a gamma distribution random number. |
762 | 762 |
/// |
763 | 763 |
///\param k shape parameter (<tt>k>0</tt> integer) |
764 | 764 |
double gamma(int k) |
765 | 765 |
{ |
766 | 766 |
double s = 0; |
767 | 767 |
for(int i=0;i<k;i++) s-=std::log(1.0-real<double>()); |
768 | 768 |
return s; |
769 | 769 |
} |
770 | 770 |
|
771 | 771 |
/// Gamma distribution with given shape and scale parameter |
772 | 772 |
|
773 | 773 |
/// This function generates a gamma distribution random number. |
774 | 774 |
/// |
775 | 775 |
///\param k shape parameter (<tt>k>0</tt>) |
776 | 776 |
///\param theta scale parameter |
777 | 777 |
/// |
778 | 778 |
double gamma(double k,double theta=1.0) |
779 | 779 |
{ |
780 | 780 |
double xi,nu; |
781 | 781 |
const double delta = k-std::floor(k); |
782 | 782 |
const double v0=E/(E-delta); |
783 | 783 |
do { |
784 | 784 |
double V0=1.0-real<double>(); |
785 | 785 |
double V1=1.0-real<double>(); |
786 | 786 |
double V2=1.0-real<double>(); |
787 | 787 |
if(V2<=v0) |
788 | 788 |
{ |
789 | 789 |
xi=std::pow(V1,1.0/delta); |
790 | 790 |
nu=V0*std::pow(xi,delta-1.0); |
791 | 791 |
} |
792 | 792 |
else |
793 | 793 |
{ |
794 | 794 |
xi=1.0-std::log(V1); |
795 | 795 |
nu=V0*std::exp(-xi); |
796 | 796 |
} |
797 | 797 |
} while(nu>std::pow(xi,delta-1.0)*std::exp(-xi)); |
798 |
return theta*(xi |
|
798 |
return theta*(xi+gamma(int(std::floor(k)))); |
|
799 | 799 |
} |
800 | 800 |
|
801 | 801 |
/// Weibull distribution |
802 | 802 |
|
803 | 803 |
/// This function generates a Weibull distribution random number. |
804 | 804 |
/// |
805 | 805 |
///\param k shape parameter (<tt>k>0</tt>) |
806 | 806 |
///\param lambda scale parameter (<tt>lambda>0</tt>) |
807 | 807 |
/// |
808 | 808 |
double weibull(double k,double lambda) |
809 | 809 |
{ |
810 | 810 |
return lambda*pow(-std::log(1.0-real<double>()),1.0/k); |
811 | 811 |
} |
812 | 812 |
|
813 | 813 |
/// Pareto distribution |
814 | 814 |
|
815 | 815 |
/// This function generates a Pareto distribution random number. |
816 | 816 |
/// |
817 | 817 |
///\param k shape parameter (<tt>k>0</tt>) |
818 | 818 |
///\param x_min location parameter (<tt>x_min>0</tt>) |
819 | 819 |
/// |
820 | 820 |
double pareto(double k,double x_min) |
821 | 821 |
{ |
822 |
return exponential(gamma(k,1.0/x_min)); |
|
822 |
return exponential(gamma(k,1.0/x_min))+x_min; |
|
823 | 823 |
} |
824 | 824 |
|
825 | 825 |
/// Poisson distribution |
826 | 826 |
|
827 | 827 |
/// This function generates a Poisson distribution random number with |
828 | 828 |
/// parameter \c lambda. |
829 | 829 |
/// |
830 | 830 |
/// The probability mass function of this distribusion is |
831 | 831 |
/// \f[ \frac{e^{-\lambda}\lambda^k}{k!} \f] |
832 | 832 |
/// \note The algorithm is taken from the book of Donald E. Knuth titled |
833 | 833 |
/// ''Seminumerical Algorithms'' (1969). Its running time is linear in the |
834 | 834 |
/// return value. |
835 | 835 |
|
836 | 836 |
int poisson(double lambda) |
837 | 837 |
{ |
838 | 838 |
const double l = std::exp(-lambda); |
839 | 839 |
int k=0; |
840 | 840 |
double p = 1.0; |
841 | 841 |
do { |
842 | 842 |
k++; |
843 | 843 |
p*=real<double>(); |
844 | 844 |
} while (p>=l); |
845 | 845 |
return k-1; |
846 | 846 |
} |
847 | 847 |
|
848 | 848 |
///@} |
849 | 849 |
|
850 | 850 |
///\name Two dimensional distributions |
851 | 851 |
/// |
852 | 852 |
|
853 | 853 |
///@{ |
854 | 854 |
|
855 | 855 |
/// Uniform distribution on the full unit circle |
856 | 856 |
|
857 | 857 |
/// Uniform distribution on the full unit circle. |
858 | 858 |
/// |
859 | 859 |
dim2::Point<double> disc() |
860 | 860 |
{ |
861 | 861 |
double V1,V2; |
862 | 862 |
do { |
863 | 863 |
V1=2*real<double>()-1; |
864 | 864 |
V2=2*real<double>()-1; |
865 | 865 |
|
866 | 866 |
} while(V1*V1+V2*V2>=1); |
867 | 867 |
return dim2::Point<double>(V1,V2); |
868 | 868 |
} |
869 | 869 |
/// A kind of two dimensional Gauss distribution |
870 | 870 |
|
871 | 871 |
/// This function provides a turning symmetric two-dimensional distribution. |
872 | 872 |
/// Both coordinates are of standard normal distribution, but they are not |
873 | 873 |
/// independent. |
874 | 874 |
/// |
875 | 875 |
/// \note The coordinates are the two random variables provided by |
876 | 876 |
/// the Box-Muller method. |
877 | 877 |
dim2::Point<double> gauss2() |
878 | 878 |
{ |
879 | 879 |
double V1,V2,S; |
880 | 880 |
do { |
881 | 881 |
V1=2*real<double>()-1; |
882 | 882 |
V2=2*real<double>()-1; |
883 | 883 |
S=V1*V1+V2*V2; |
884 | 884 |
} while(S>=1); |
885 | 885 |
double W=std::sqrt(-2*std::log(S)/S); |
886 | 886 |
return dim2::Point<double>(W*V1,W*V2); |
887 | 887 |
} |
888 | 888 |
/// A kind of two dimensional exponential distribution |
889 | 889 |
|
890 | 890 |
/// This function provides a turning symmetric two-dimensional distribution. |
891 | 891 |
/// The x-coordinate is of conditionally exponential distribution |
892 | 892 |
/// with the condition that x is positive and y=0. If x is negative and |
893 | 893 |
/// y=0 then, -x is of exponential distribution. The same is true for the |
894 | 894 |
/// y-coordinate. |
895 | 895 |
dim2::Point<double> exponential2() |
896 | 896 |
{ |
897 | 897 |
double V1,V2,S; |
898 | 898 |
do { |
899 | 899 |
V1=2*real<double>()-1; |
900 | 900 |
V2=2*real<double>()-1; |
901 | 901 |
S=V1*V1+V2*V2; |
902 | 902 |
} while(S>=1); |
903 | 903 |
double W=-std::log(S)/S; |
904 | 904 |
return dim2::Point<double>(W*V1,W*V2); |
905 | 905 |
} |
906 | 906 |
|
907 | 907 |
///@} |
908 | 908 |
}; |
909 | 909 |
|
910 | 910 |
|
911 | 911 |
extern Random rnd; |
912 | 912 |
|
913 | 913 |
} |
914 | 914 |
|
915 | 915 |
#endif |
0 comments (0 inline)