source: vscpl/src/hu/varadiistvan/scpl/PseudoRandom.h@ 10:1ee8d0a23099

Last change on this file since 10:1ee8d0a23099 was 8:7b2598d1ea55, checked in by István Váradi <ivaradi@…>, 12 years ago

Imported the I/O library for POSIX and the pseudo-random generator

File size: 5.4 KB
Line 
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
40namespace hu { namespace varadiistvan { namespace scpl {
41
42//------------------------------------------------------------------------------
43
44/**
45 * A pseudo-random number generator. The algorithm is the same as
46 * described in http://www.mathstat.dal.ca/~selinger/random, i.e. the
47 * GNU libc random number generator. It is reimplemented, because
48 * Windows does not provide a thread-safe, reentrable pseudo-random
49 * number generation facility, so it should have been implemented for
50 * Win32 anyway.
51 */
52class PseudoRandom
53{
54public:
55 /**
56 * The largest random number generated in the unconverted
57 * sequence.
58 */
59 static const uint32_t MAX = (1<<31);
60
61private:
62 /**
63 * The size of the vector.
64 */
65 static const size_t vectorSize = 34;
66
67 /**
68 * The number of random numbers thrown away.
69 */
70 static const size_t numDiscarded = 344;
71
72 /**
73 * The vector of random numbers used to generate the next one.
74 */
75 uint32_t vector[vectorSize];
76
77 /**
78 * The index of the next random number.
79 */
80 int nextIndex;
81
82public:
83 /**
84 * Construct the random number generator with a default seed
85 * (taken by calling time()).
86 */
87 PseudoRandom();
88
89 /**
90 * Construct the random number generator with the given seed.
91 */
92 PseudoRandom(uint32_t seed);
93
94 /**
95 * Get the next raw value.
96 */
97 uint32_t next();
98
99 /**
100 * Get the next random number as an unsigned value between [from, to].
101 */
102 unsigned nextUnsigned(unsigned to = MAX, unsigned from=0);
103
104 /**
105 * Get the next random number as a double value between [from,
106 * to].
107 */
108 double nextDouble(double to = 1.0, double from= 0.0);
109
110private:
111 /**
112 * Initialize the random number generator with the given seed.
113 */
114 void initialize(uint32_t seed);
115};
116
117//------------------------------------------------------------------------------
118// Inline definitions
119//------------------------------------------------------------------------------
120
121inline PseudoRandom::PseudoRandom()
122{
123 initialize(time(0));
124}
125
126//------------------------------------------------------------------------------
127
128inline PseudoRandom::PseudoRandom(uint32_t seed)
129{
130 initialize(seed);
131}
132
133//------------------------------------------------------------------------------
134
135inline 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
150inline 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
158inline 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:
Note: See TracBrowser for help on using the repository browser.