00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef __ENKI_RANDOM_H
00035 #define __ENKI_RANDOM_H
00036
00037 #include <cmath>
00038 #include <cstdlib>
00039
00044 namespace Enki
00045 {
00047
00048 class FastRandom
00049 {
00050 private:
00051 unsigned long randx;
00052
00053 public:
00055 FastRandom(void) { randx = 0; }
00057 void setSeed(unsigned long seed) { randx = seed; }
00059 unsigned long get(void) { return (randx = randx*1103515245 + 12345) & 0x7fffffff; }
00061 double getRange(double range) { return (static_cast<double>(get()) * range) / 2147483648.0; }
00062 };
00063
00065
00066 inline double uniformRand(void)
00067 {
00068 return double(rand())/RAND_MAX;
00069 }
00070
00072 struct UniformRand
00073 {
00074 double from;
00075 double to;
00076
00078 UniformRand(double from = 0.0, double to = 1.0) { this->from = from; this->to = to; }
00079
00081 double operator()() const { return from + (to-from)*uniformRand(); }
00082 };
00083
00085
00086 inline unsigned intRand(unsigned max)
00087 {
00088 if (max)
00089 return rand() % max;
00090 else
00091 return 0;
00092 }
00093
00095
00096 inline bool boolRand(double prob = 0.5)
00097 {
00098 return uniformRand() < prob;
00099 }
00100
00102
00103 inline double gaussianRand(double mean, double sigm)
00104 {
00105
00106
00107
00108
00109
00110 double r, x, y;
00111
00112
00113 do
00114 {
00115 x = uniformRand()*2 - 1;
00116 y = uniformRand()*2 - 1;
00117 r = x*x + y*y;
00118 }
00119 while (r > 1.0 || r == 0);
00120
00121
00122 return sigm * y * sqrt (-2.0 * log(r) / r) + mean;
00123 }
00124 }
00125
00126 #endif