00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef SPOT_MISC_RANDOM_HH
00023 # define SPOT_MISC_RANDOM_HH
00024
00025 # include <cmath>
00026
00027 namespace spot
00028 {
00032
00036 void srand(unsigned int seed);
00037
00042 int rrand(int min, int max);
00043
00048 int mrand(int max);
00049
00054 double drand();
00055
00062 double nrand();
00063
00069 double bmrand();
00070
00082 template<double (*gen)()>
00083 class barand
00084 {
00085 public:
00086 barand(int n, double p)
00087 : n_(n), m_(n * p), s_(sqrt(n * p * (1 - p)))
00088 {
00089 }
00090
00091 int
00092 rand() const
00093 {
00094 int res;
00095
00096 for (;;)
00097 {
00098 double x = gen() * s_ + m_;
00099 if (x < 0.0)
00100 continue;
00101 res = static_cast<int> (x);
00102 if (res <= n_)
00103 break;
00104 }
00105 return res;
00106 }
00107 protected:
00108 const int n_;
00109 const double m_;
00110 const double s_;
00111 };
00112
00117 int prand(double p);
00119 }
00120
00121 #endif // SPOT_MISC_RANDOM_HH