source: xplcommon/src/xplcommon/PseudoRandom.h@ 27:11b3ec33029d

Last change on this file since 27:11b3ec33029d was 27:11b3ec33029d, checked in by István Váradi <ivaradi@…>, 11 years ago

Implemented a pseudo-random number generator

File size: 5.4 KB
Line 
1// Copyright (c) 2013 by István Váradi
2
3// This file is part of libxplcommon, a common utility library for
4// development related to X-Plane
5
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
13// and/or other materials provided with the distribution.
14
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26// The views and conclusions contained in the software and documentation are those
27// of the authors and should not be interpreted as representing official policies,
28// either expressed or implied, of the FreeBSD Project.
29
30#ifndef XPLCOMMON_PSEUDORANDOM_H
31#define XPLCOMMON_PSEUDORANDOM_H
32//------------------------------------------------------------------------------
33
34#include <cstdlib>
35#include <ctime>
36
37#include <inttypes.h>
38
39//------------------------------------------------------------------------------
40
41namespace xplcommon {
42
43//------------------------------------------------------------------------------
44
45/**
46 * A pseudo-random number generator. The algorithm is the same as
47 * described in http://www.mathstat.dal.ca/~selinger/random, i.e. the
48 * GNU libc random number generator. It is reimplemented, because
49 * Windows does not provide a thread-safe, reentrable pseudo-random
50 * number generation facility, so it should have been implemented for
51 * Win32 anyway.
52 */
53class PseudoRandom
54{
55public:
56 /**
57 * The largest random number generated in the unconverted
58 * sequence.
59 */
60 static const uint32_t MAX = (1<<31);
61
62private:
63 /**
64 * The size of the vector.
65 */
66 static const size_t vectorSize = 34;
67
68 /**
69 * The number of random numbers thrown away.
70 */
71 static const size_t numDiscarded = 344;
72
73 /**
74 * The vector of random numbers used to generate the next one.
75 */
76 uint32_t vector[vectorSize];
77
78 /**
79 * The index of the next random number.
80 */
81 int nextIndex;
82
83public:
84 /**
85 * Construct the random number generator with a default seed
86 * (taken by calling time()).
87 */
88 PseudoRandom();
89
90 /**
91 * Construct the random number generator with the given seed.
92 */
93 PseudoRandom(uint32_t seed);
94
95 /**
96 * Get the next raw value.
97 */
98 uint32_t next();
99
100 /**
101 * Get the next random number as an unsigned value between [from, to].
102 */
103 unsigned nextUnsigned(unsigned to = MAX, unsigned from=0);
104
105 /**
106 * Get the next random number as a double value between [from,
107 * to].
108 */
109 double nextDouble(double to = 1.0, double from= 0.0);
110
111private:
112 /**
113 * Initialize the random number generator with the given seed.
114 */
115 void initialize(uint32_t seed);
116};
117
118//------------------------------------------------------------------------------
119// Inline definitions
120//------------------------------------------------------------------------------
121
122inline PseudoRandom::PseudoRandom()
123{
124 initialize(time(0));
125}
126
127//------------------------------------------------------------------------------
128
129inline PseudoRandom::PseudoRandom(uint32_t seed)
130{
131 initialize(seed);
132}
133
134//------------------------------------------------------------------------------
135
136inline uint32_t PseudoRandom::next()
137{
138 int i1 = (nextIndex<3) ? (vectorSize + nextIndex - 3) : (nextIndex - 3);
139 int i2 = (nextIndex<31) ? (vectorSize + nextIndex - 31) : (nextIndex - 31);
140
141 vector[nextIndex] = vector[i1] + vector[i2];
142 uint32_t x = vector[nextIndex]>>1;
143
144 ++nextIndex; if (nextIndex>=static_cast<int>(vectorSize)) nextIndex = 0;
145
146 return x;
147}
148
149//------------------------------------------------------------------------------
150
151inline unsigned PseudoRandom::nextUnsigned(unsigned to, unsigned from)
152{
153 return from + static_cast<unsigned>(static_cast<double>(to - from) * next()
154 / static_cast<double>(MAX));
155}
156
157//------------------------------------------------------------------------------
158
159inline double PseudoRandom::nextDouble(double to, double from)
160{
161 return from + (to-from) * next() / static_cast<double>(MAX);
162}
163
164//------------------------------------------------------------------------------
165//------------------------------------------------------------------------------
166
167} /* namespace xplcommon */
168
169//------------------------------------------------------------------------------
170#endif // XPLCOMMON_PSEUDORANDOM_H
171
172// Local Variables:
173// mode: C++
174// c-basic-offset: 4
175// indent-tabs-mode: nil
176// End:
Note: See TracBrowser for help on using the repository browser.