1 /*! ========================================================================
2 ** Extended Template and Library
3 ** Random Number Generator Class Implementation
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
23 ** ========================================================================= */
25 /* === S T A R T =========================================================== */
27 #ifndef __ETL__RANDOM_H
28 #define __ETL__RANDOM_H
30 /* === H E A D E R S ======================================================= */
32 /* === M A C R O S ========================================================= */
34 /* === C L A S S E S & S T R U C T S ======================================= */
42 typedef int seed_type;
43 typedef short value_type;
46 short entropy_pool[256];
56 void seed(const seed_type &x)
59 void add_entropy(value_type entropy)
62 for(i=0;i<POOL_SIZE;i++)
63 entropy^=(entropy_pool[i]^=entropy*i);
66 void add_entropy(const value_type *entropy, int size)
72 if(pool_index>POOL_SIZE)
75 return entropy_pool[pool_index++]%mod+offset;
76 return entropy_pool[pool_index++];
81 template <class T,int POOL_SIZE=256>
86 typedef int seed_type;
89 value_type entropy_pool[POOL_SIZE];
92 value_type mod,offset;
101 void seed(const seed_type &x)
104 void set_range(const value_type &floor,const value_type &ceil)
105 { mod=ceil-floor; offset=floor; }
107 void set_range(const value_type &ceil)
110 void add_entropy(value_type entropy)
113 for(i=0;i<POOL_SIZE;i++)
114 entropy^=(entropy_pool[i]^=entropy*i);
117 void add_entropy(const char *entropy)
121 value_type operator()(void)
123 if(pool_index>POOL_SIZE)
126 return entropy_pool[pool_index++]%mod+offset;
127 return entropy_pool[pool_index++];
131 /* === T Y P E D E F S ===================================================== */
135 /* === E N D =============================================================== */