00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #pragma once
00025 #ifndef __D_RANDOM__
00026 #define __D_RANDOM__
00027
00028 #include <cstdlib>
00029 #include <vector>
00030
00031 namespace DUtils {
00032
00034 class Random
00035 {
00036 public:
00037 class UnrepeatedRandomizer;
00038
00039 public:
00043 static void SeedRand();
00044
00049 static void SeedRandOnce();
00050
00055 static void SeedRand(int seed);
00056
00062 static void SeedRandOnce(int seed);
00063
00068 template <class T>
00069 static T RandomValue(){
00070 return (T)rand()/(T)RAND_MAX;
00071 }
00072
00079 template <class T>
00080 static T RandomValue(T min, T max){
00081 return Random::RandomValue<T>() * (max - min) + min;
00082 }
00083
00090 static int RandomInt(int min, int max);
00091
00097 template <class T>
00098 static T RandomGaussianValue(T mean, T sigma)
00099 {
00100
00101 T x1, x2, w, y1;
00102
00103 do {
00104 x1 = (T)2. * RandomValue<T>() - (T)1.;
00105 x2 = (T)2. * RandomValue<T>() - (T)1.;
00106 w = x1 * x1 + x2 * x2;
00107 } while ( w >= (T)1. || w == (T)0. );
00108
00109 w = sqrt( ((T)-2.0 * log( w ) ) / w );
00110 y1 = x1 * w;
00111
00112 return( mean + y1 * sigma );
00113 }
00114
00115 private:
00116
00118 static bool m_already_seeded;
00119
00120 };
00121
00122
00123
00125 class Random::UnrepeatedRandomizer
00126 {
00127 public:
00128
00134 UnrepeatedRandomizer(int min, int max);
00135 ~UnrepeatedRandomizer(){}
00136
00141 UnrepeatedRandomizer(const UnrepeatedRandomizer& rnd);
00142
00147 UnrepeatedRandomizer& operator=(const UnrepeatedRandomizer& rnd);
00148
00154 int get();
00155
00162 inline bool empty() const { return m_values.empty(); }
00163
00168 inline unsigned int left() const { return m_values.size(); }
00169
00173 void reset();
00174
00175 protected:
00176
00180 void createValues();
00181
00182 protected:
00183
00185 int m_min;
00187 int m_max;
00188
00190 std::vector<int> m_values;
00191
00192 };
00193
00194 }
00195
00196 #endif
00197