Very Simple Cross-Platform Library
PseudoRandom.h
1 // Copyright (c) 2013 by István Váradi
2 
3 // This file is part of VSCPL, a simple cross-platform utility library
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 
8 // 1. Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 // The views and conclusions contained in the software and documentation are those
26 // of the authors and should not be interpreted as representing official policies,
27 // either expressed or implied, of the FreeBSD Project.
28 
29 #ifndef HU_VARADIISTVAN_SCPL_PSEUDORANDOM_H
30 #define HU_VARADIISTVAN_SCPL_PSEUDORANDOM_H
31 //------------------------------------------------------------------------------
32 
33 #include <cstdlib>
34 #include <ctime>
35 
36 #include <inttypes.h>
37 
38 //------------------------------------------------------------------------------
39 
40 namespace hu { namespace varadiistvan { namespace scpl {
41 
42 //------------------------------------------------------------------------------
43 
53 {
54 public:
59  static const uint32_t MAX = (1<<31);
60 
61 private:
65  static const size_t vectorSize = 34;
66 
70  static const size_t numDiscarded = 344;
71 
75  uint32_t vector[vectorSize];
76 
80  int nextIndex;
81 
82 public:
87  PseudoRandom();
88 
92  PseudoRandom(uint32_t seed);
93 
97  uint32_t next();
98 
102  unsigned nextUnsigned(unsigned to = MAX, unsigned from=0);
103 
108  double nextDouble(double to = 1.0, double from= 0.0);
109 
110 private:
114  void initialize(uint32_t seed);
115 };
116 
117 //------------------------------------------------------------------------------
118 // Inline definitions
119 //------------------------------------------------------------------------------
120 
122 {
123  initialize(time(0));
124 }
125 
126 //------------------------------------------------------------------------------
127 
128 inline PseudoRandom::PseudoRandom(uint32_t seed)
129 {
130  initialize(seed);
131 }
132 
133 //------------------------------------------------------------------------------
134 
135 inline uint32_t PseudoRandom::next()
136 {
137  int i1 = (nextIndex<3) ? (vectorSize + nextIndex - 3) : (nextIndex - 3);
138  int i2 = (nextIndex<31) ? (vectorSize + nextIndex - 31) : (nextIndex - 31);
139 
140  vector[nextIndex] = vector[i1] + vector[i2];
141  uint32_t x = vector[nextIndex]>>1;
142 
143  ++nextIndex; if (nextIndex>=static_cast<int>(vectorSize)) nextIndex = 0;
144 
145  return x;
146 }
147 
148 //------------------------------------------------------------------------------
149 
150 inline unsigned PseudoRandom::nextUnsigned(unsigned to, unsigned from)
151 {
152  return from + static_cast<unsigned>(static_cast<double>(to - from) * next()
153  / static_cast<double>(MAX));
154 }
155 
156 //------------------------------------------------------------------------------
157 
158 inline double PseudoRandom::nextDouble(double to, double from)
159 {
160  return from + (to-from) * next() / static_cast<double>(MAX);
161 }
162 
163 //------------------------------------------------------------------------------
164 
165 } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
166 
167 //------------------------------------------------------------------------------
168 #endif // HU_VARADIISTVAN_SCPL_PSEUDORANDOM_H
169 
170 // Local Variables:
171 // mode: C++
172 // c-basic-offset: 4
173 // indent-tabs-mode: nil
174 // End:
double nextDouble(double to=1.0, double from=0.0)
Definition: PseudoRandom.h:158
unsigned nextUnsigned(unsigned to=MAX, unsigned from=0)
Definition: PseudoRandom.h:150