123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- #pragma once
- #ifndef __D_RANDOM__
- #define __D_RANDOM__
- #include <cstdlib>
- #include <vector>
- namespace DUtils {
- class Random
- {
- public:
- class UnrepeatedRandomizer;
-
- public:
-
- static void SeedRand();
-
-
- static void SeedRandOnce();
-
- static void SeedRand(int seed);
-
- static void SeedRandOnce(int seed);
-
- template <class T>
- static T RandomValue(){
- return (T)rand()/(T)RAND_MAX;
- }
-
- template <class T>
- static T RandomValue(T min, T max){
- return Random::RandomValue<T>() * (max - min) + min;
- }
-
- static int RandomInt(int min, int max);
-
-
- template <class T>
- static T RandomGaussianValue(T mean, T sigma)
- {
-
- T x1, x2, w, y1;
- do {
- x1 = (T)2. * RandomValue<T>() - (T)1.;
- x2 = (T)2. * RandomValue<T>() - (T)1.;
- w = x1 * x1 + x2 * x2;
- } while ( w >= (T)1. || w == (T)0. );
- w = sqrt( ((T)-2.0 * log( w ) ) / w );
- y1 = x1 * w;
- return( mean + y1 * sigma );
- }
- private:
-
- static bool m_already_seeded;
-
- };
- class Random::UnrepeatedRandomizer
- {
- public:
-
- UnrepeatedRandomizer(int min, int max);
- ~UnrepeatedRandomizer(){}
-
-
- UnrepeatedRandomizer(const UnrepeatedRandomizer& rnd);
-
-
- UnrepeatedRandomizer& operator=(const UnrepeatedRandomizer& rnd);
-
-
- int get();
-
-
- inline bool empty() const { return m_values.empty(); }
-
-
- inline unsigned int left() const { return m_values.size(); }
-
-
- void reset();
-
- protected:
-
- void createValues();
- protected:
-
- int m_min;
-
- int m_max;
-
- std::vector<int> m_values;
- };
- }
- #endif
|