Library of Assembled Shared Sources
random.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2022 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @defgroup Random
46 *
47 * A set of random number generators.
48 *
49 * - @ref RandomStandard : uses the C standard function rand().
50 * - @ref RandomParkMiller : Minimal Standard generator by Park and Miller.
51 * - @ref RandomMT19937 : uses a mersenne twister MT19937.
52 */
53
54#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_RANDOM_H
55#define LASS_GUARDIAN_OF_INCLUSION_NUM_RANDOM_H
56
57#include "num_common.h"
58#include <stdlib.h>
59#include "../util/call_traits.h"
60
61namespace lass
62{
63namespace num
64{
65
66/** Uses the C standard library function rand().
67 * @ingroup Random
68 *
69 * @note this one isn't thread safe since state is stored externally.
70 */
72{
73public:
74 using result_type = int; /**< type of return value. */
75 typedef int TValue; /**< type of return value. @deprecated */
76
77 static constexpr result_type min() { return 0; } /**< minimum return value. */
78 static constexpr result_type max() { return RAND_MAX; } /**< maximum return value. */
79
80 result_type operator()() const;
81 result_type operator()(result_type supremum) const;
82
83 template <typename OutputIterator> OutputIterator getState(OutputIterator first) const;
84 template <typename InputIterator> void setState(InputIterator first, InputIterator last);
85
86
87};
88
89
90
91/** Minimal Standard generator by Park and Miller
92 * @ingroup Random
93 *
94 * RandomParkMiller is the LASS implementation of the Minimal Standard generator by Park and Miller
95 *
96 * <i>Park and Miller, "Random Number Generators: Good ones are hard to find", Communications of the
97 * ACM, October 1988, Volume 31, No 10, pages 1192-1201.</i>
98 */
100 [[deprecated("RandomParkMiller is deprecated, use std::minstd_rand0 instead, or upgrade to std::minstd_rand")]]
102{
103public:
104 using result_type = num::Tuint32; /**< type of return value. */
105 typedef num::Tuint32 TValue; /**< type of return value. @deprecated */
106
107 static constexpr result_type min() { return 0; } /**< minimum return value. */
108 static constexpr result_type max() { return modulus_ - 1; } /**< maximum return value. */
109
111 explicit RandomParkMiller(result_type seed);
112
113 void seed(result_type seed);
114
115 result_type operator()();
116 result_type operator()(result_type supremum);
117
118 template <typename OutputIterator> OutputIterator getState(OutputIterator first) const;
119 template <typename InputIterator> void setState(InputIterator first, InputIterator last);
120
121private:
122
123 enum
124 {
125 multiplier_ = 16807,
126 modulus_ = 2147483647,
127 seedMask_ = 123456789,
128 schrageQuotient_ = modulus_ / multiplier_,
129 schrageRest_ = modulus_ % multiplier_
130 };
131
132 result_type buffer_;
133};
134
135
136/** implemenents a mersenne twister MT19937.
137 * @ingroup Random
138 *
139 * RandomMT19937 is the LASS implementation of the Mersenne twister. Mersenne Twister(MT) is a
140 * pseudorandom number generator developped by Makoto Matsumoto and Takuji Nishimura (alphabetical
141 * order) during 1996-1997. MT has the following merits:
142 *
143 * - It is designed with consideration on the flaws of various existing generators.
144 * - Far longer period and far higher order of equidistribution than any other implemented
145 * generators. (It is proved that the period is 2^19937-1, and 623-dimensional equidistribution
146 * property is assured.)
147 * - Fast generation. (Although it depends on the system, it is reported that MT is sometimes
148 * faster than the standard ANSI-C library in a system with pipeline and cache memory.)
149 * - Efficient use of the memory. (The implementation consumes only 624 words of working area.)
150 *
151 * <i>M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-dimensionally equidistributed uniform
152 * pseudorandom number generator", ACM Trans. on Modeling and Computer Simulation Vol. 8, No. 1,
153 * January pp.3-30 1998</i>, http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
154 *
155 * Our version is implemented after the MT19937 standard code of 2002/1/26 (mt19937ar.c)
156 */
158 [[deprecated("RandomMT19937 is deprecated, use std::mt19937 instead, or upgrade to std::mt19937_64")]]
160{
161public:
162 using result_type = num::Tuint32; /**< type of return value. */
163 typedef num::Tuint32 TValue; /**< type of return value. @deprecated */
164
165 static constexpr result_type min() { return 0; } /**< minimum return value. */
166 static constexpr result_type max() { return 0xffffffff; } /**< maximum return value. */
167
169 explicit RandomMT19937(result_type seed);
170 template <typename ForwardIterator> RandomMT19937(ForwardIterator first, ForwardIterator last);
171
172 void seed(result_type seed);
173 template <typename ForwardIterator> void seed(ForwardIterator first, ForwardIterator last);
174
175 result_type operator()();
176 result_type operator()(result_type supremum);
177
178 template <typename OutputIterator> OutputIterator getState(OutputIterator first) const;
179 template <typename InputIterator> void setState(InputIterator first, InputIterator last);
180
181private:
182
183 LASS_META_ASSERT(sizeof(result_type) * lass::bitsPerByte == 32,
184 MersenneTwister_is_designed_to_be_a_32_bits_random_number_generator);
185
186 enum
187 {
188 stateSize_ = 624, /**< size of state vector */
189 shiftSize_ = 397
190 };
191
192 void reload();
193 result_type twist(result_type a, result_type b, result_type c) const;
194
195 result_type state_[stateSize_]; /**< the array for the state vector. */
196 result_type index_; /**< index in state vector. */
197
198 static result_type wordMask_;
199 static result_type lowerMask_;
200 static result_type upperMask_;
201};
202
203
204/** xorshift128+ pseudorandom number generator
205* @ingroup Random
206*
207* Variation on xorshift generator that avoids the linear artifacts the
208* xorshift generators are prone to. In contrast to the XSadd variants,
209* it also passes the BigCrush tests even when bit reversed!
210* As a disadvantage, xorshift128+ is only 1-dimensionally equidistributed
211* while the underlying xorshift128 is 2-dimensionally equidistributed.
212*
213* This implementation uses 128 bits of state with following shifts: a=23, b=17, c=26.
214* It has a maximal period of 2^128-1
215*
216* <i>S. Vigna, "Further scramblings of Marsaglia's xorshift generators",
217* Dec 2015, arXiv:1404.0390v2 [cs.DS]</i>, http://arxiv.org/abs/1404.0390v2
218*/
219class LASS_DLL RandomXorShift128Plus
220{
221public:
222 using result_type = num::Tuint64; /**< type of return value. */
223 typedef num::Tuint64 TValue; /**< type of return value. @deprecated */
224
225 static constexpr result_type min() { return 0; } /**< minimum return value. */
226 static constexpr result_type max() { return 0xffffffffffffffff; } /**< maximum return value. */
227
229 explicit RandomXorShift128Plus(result_type seed);
230
231 result_type operator()();
232
233 void seed(result_type index);
234
235private:
236 result_type state_[2];
237};
238
239
240/** Halton sequence
241 * @ingroup Random
242 *
243 * Determinstic quasi-random number sequence of low-discrepancy,
244 * using the radical inverse of an increasing index in a given base.
245 *
246 * @warning this gives a very predictable and unrandom sequence!
247 * But, it's very good for usage with Monte Carlo integration.
248 * Very usefull to build multi-dimensional Halton sequences.
249 */
250class LASS_DLL RandomRadicalInverse
251{
252public:
253 using result_type = double; /**< type of return value. */
254 typedef double TValue; /**< type of return value. @deprecated */
255
256 static constexpr result_type min() { return 0.0; } /**< minimum return value. */
257 static constexpr result_type max() { return 1.0; } /**< maximum return value. */
258
259 explicit RandomRadicalInverse(size_t base);
260
261 result_type operator()();
262
263 void seed(size_t index);
264
265private:
266 size_t index_;
267 size_t base_;
268 result_type invBase_;
269};
270
271
272/** implemenents xkcd's fair dice roll random number generator
273* @ingroup Random
274*
275* http://xkcd.com/221/
276*/
278{
279public:
280 using result_type = int; /**< type of return value. */
281 typedef int TValue; /**< type of return value. @deprecated */
282
283 static constexpr result_type min() { return 0; } /**< minimum return value. */
284 static constexpr result_type max() { return 5; } /**< maximum return value. */
285
286 result_type operator()();
287};
288
289
290}
291}
292
293#include "random.inl"
294
295#endif
296
297// EOF
implemenents a mersenne twister MT19937.
Definition random.h:160
num::Tuint32 TValue
type of return value.
Definition random.h:163
static constexpr result_type max()
maximum return value.
Definition random.h:166
static constexpr result_type min()
minimum return value.
Definition random.h:165
num::Tuint32 result_type
type of return value.
Definition random.h:162
RandomMT19937()
default constructor.
Definition random.cpp:104
Minimal Standard generator by Park and Miller.
Definition random.h:102
num::Tuint32 TValue
type of return value.
Definition random.h:105
num::Tuint32 result_type
type of return value.
Definition random.h:104
static constexpr result_type max()
maximum return value.
Definition random.h:108
RandomParkMiller()
default constructor.
Definition random.cpp:64
static constexpr result_type min()
minimum return value.
Definition random.h:107
double result_type
type of return value.
Definition random.h:253
double TValue
type of return value.
Definition random.h:254
static constexpr result_type max()
maximum return value.
Definition random.h:257
static constexpr result_type min()
minimum return value.
Definition random.h:256
Uses the C standard library function rand().
Definition random.h:72
int TValue
type of return value.
Definition random.h:75
static constexpr result_type min()
minimum return value.
Definition random.h:77
static constexpr result_type max()
maximum return value.
Definition random.h:78
int result_type
type of return value.
Definition random.h:74
implemenents xkcd's fair dice roll random number generator
Definition random.h:278
static constexpr result_type max()
maximum return value.
Definition random.h:284
int TValue
type of return value.
Definition random.h:281
int result_type
type of return value.
Definition random.h:280
static constexpr result_type min()
minimum return value.
Definition random.h:283
xorshift128+ pseudorandom number generator
Definition random.h:220
static constexpr result_type min()
minimum return value.
Definition random.h:225
num::Tuint64 result_type
type of return value.
Definition random.h:222
static constexpr result_type max()
maximum return value.
Definition random.h:226
num::Tuint64 TValue
type of return value.
Definition random.h:223
#define LASS_DLL
DLL interface: import or export symbols?
#define LASS_META_ASSERT(expression, message)
complite time static check
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53