73efa08c04ad803e60b2b70d27628d84559e88c9
[synfig.git] / ETL / tags / stable / ETL / _random.h
1 /*! ========================================================================
2 ** Extended Template and Library
3 ** Random Number Generator Class Implementation
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
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.
12 **
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.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
22 **
23 ** ========================================================================= */
24
25 /* === S T A R T =========================================================== */
26
27 #ifndef __ETL__RANDOM_H
28 #define __ETL__RANDOM_H
29
30 /* === H E A D E R S ======================================================= */
31
32 /* === M A C R O S ========================================================= */
33
34 /* === C L A S S E S & S T R U C T S ======================================= */
35
36 _ETL_BEGIN_NAMESPACE
37
38 /*
39 class rand_source_xor
40 {
41 public:
42         typedef int seed_type;
43         typedef short value_type;
44
45 private:
46         short entropy_pool[256];
47         int pool_index;
48
49 public:
50         random()
51         {
52                 seed(0);
53                 mod=offset=0;
54         }
55
56         void seed(const seed_type &x)
57         { pool_index=0; }
58
59         void add_entropy(value_type entropy)
60         {
61                 int i;
62                 for(i=0;i<POOL_SIZE;i++)
63                         entropy^=(entropy_pool[i]^=entropy*i);
64         }
65
66         void add_entropy(const value_type *entropy, int size)
67         {
68         }
69
70         short get_short()
71         {
72                 if(pool_index>POOL_SIZE)
73                         pool_index=0;
74                 if(mod)
75                         return entropy_pool[pool_index++]%mod+offset;
76                 return entropy_pool[pool_index++];
77         }
78 };
79 */
80
81 template <class T,int POOL_SIZE=256>
82 class random
83 {
84 public:
85         typedef T value_type;
86         typedef int seed_type;
87
88 private:
89         value_type entropy_pool[POOL_SIZE];
90         int pool_index;
91
92         value_type mod,offset;
93
94 public:
95         random()
96         {
97                 seed(0);
98                 mod=offset=0;
99         }
100
101         void seed(const seed_type &x)
102         { pool_index=0; }
103
104         void set_range(const value_type &floor,const value_type &ceil)
105         { mod=ceil-floor; offset=floor; }
106
107         void set_range(const value_type &ceil)
108         { mod=ceil; }
109
110         void add_entropy(value_type entropy)
111         {
112                 int i;
113                 for(i=0;i<POOL_SIZE;i++)
114                         entropy^=(entropy_pool[i]^=entropy*i);
115         }
116
117         void add_entropy(const char *entropy)
118         {
119         }
120
121         value_type operator()(void)
122         {
123                 if(pool_index>POOL_SIZE)
124                         pool_index=0;
125                 if(mod)
126                         return entropy_pool[pool_index++]%mod+offset;
127                 return entropy_pool[pool_index++];
128         }
129 };
130
131 /* === T Y P E D E F S ===================================================== */
132
133 _ETL_END_NAMESPACE
134
135 /* === E N D =============================================================== */
136
137 #endif
138