_random.h

Go to the documentation of this file.
00001 
00025 /* === S T A R T =========================================================== */
00026 
00027 #ifndef __ETL__RANDOM_H
00028 #define __ETL__RANDOM_H
00029 
00030 /* === H E A D E R S ======================================================= */
00031 
00032 /* === M A C R O S ========================================================= */
00033 
00034 /* === C L A S S E S & S T R U C T S ======================================= */
00035 
00036 _ETL_BEGIN_NAMESPACE
00037 
00038 /*
00039 class rand_source_xor
00040 {
00041 public:
00042     typedef int seed_type;
00043     typedef short value_type;
00044 
00045 private:
00046     short entropy_pool[256];
00047     int pool_index;
00048 
00049 public:
00050     random()
00051     {
00052         seed(0);
00053         mod=offset=0;
00054     }
00055 
00056     void seed(const seed_type &x)
00057     { pool_index=0; }
00058 
00059     void add_entropy(value_type entropy)
00060     {
00061         int i;
00062         for(i=0;i<POOL_SIZE;i++)
00063             entropy^=(entropy_pool[i]^=entropy*i);
00064     }
00065 
00066     void add_entropy(const value_type *entropy, int size)
00067     {
00068     }
00069 
00070     short get_short()
00071     {
00072         if(pool_index>POOL_SIZE)
00073             pool_index=0;
00074         if(mod)
00075             return entropy_pool[pool_index++]%mod+offset;
00076         return entropy_pool[pool_index++];
00077     }
00078 };
00079 */
00080 
00081 template <class T,int POOL_SIZE=256>
00082 class random
00083 {
00084 public:
00085     typedef T value_type;
00086     typedef int seed_type;
00087 
00088 private:
00089     value_type entropy_pool[POOL_SIZE];
00090     int pool_index;
00091 
00092     value_type mod,offset;
00093 
00094 public:
00095     random()
00096     {
00097         seed(0);
00098         mod=offset=0;
00099     }
00100 
00101     void seed(const seed_type &x)
00102     { pool_index=0; }
00103 
00104     void set_range(const value_type &floor,const value_type &ceil)
00105     { mod=ceil-floor; offset=floor; }
00106 
00107     void set_range(const value_type &ceil)
00108     { mod=ceil; }
00109 
00110     void add_entropy(value_type entropy)
00111     {
00112         int i;
00113         for(i=0;i<POOL_SIZE;i++)
00114             entropy^=(entropy_pool[i]^=entropy*i);
00115     }
00116 
00117     void add_entropy(const char *entropy)
00118     {
00119     }
00120 
00121     value_type operator()(void)
00122     {
00123         if(pool_index>POOL_SIZE)
00124             pool_index=0;
00125         if(mod)
00126             return entropy_pool[pool_index++]%mod+offset;
00127         return entropy_pool[pool_index++];
00128     }
00129 };
00130 
00131 /* === T Y P E D E F S ===================================================== */
00132 
00133 _ETL_END_NAMESPACE
00134 
00135 /* === E N D =============================================================== */
00136 
00137 #endif
00138 

Generated on Sun Oct 28 06:22:06 2007 for ETL by  doxygen 1.5.3-20071008