forked from catid/libcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MersenneTwister.hpp
157 lines (124 loc) · 5.07 KB
/
MersenneTwister.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LibCat nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
Algorithm by Makoto Matsumoto
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
*/
#ifndef CAT_MERSENNE_TWISTER_HPP
#define CAT_MERSENNE_TWISTER_HPP
#include "BitMath.hpp"
namespace cat {
// Noncryptographic pseudo-random number generator
class CAT_EXPORT MersenneTwister
{
static const u32 MEXP = 19937;
static const u32 N128 = MEXP/128 + 1;
static const u32 N64 = N128 * 2;
static const u32 N32 = N128 * 4;
static const u32 POS1 = 122;
static const u32 SL1 = 18;
static const u32 SL2 = 1;
static const u32 SL2BITS = SL2*8;
static const u32 SR1 = 11;
static const u32 SR2 = 1;
static const u32 SR2BITS = SR2*8;
static const u32 MSK1 = 0xdfffffefU;
static const u32 MSK2 = 0xddfecb7fU;
static const u32 MSK3 = 0xbffaffffU;
static const u32 MSK4 = 0xbffffff6U;
struct MT128 {
u32 u[4];
};
MT128 state[19937/128 + 1];
u32 *state32;
u32 used;
CAT_INLINE void shiftLeft128(MT128 *r, MT128 *n, u32 bits);
CAT_INLINE void shiftRight128(MT128 *r, MT128 *n, u32 bits);
void enforcePeriod(); // make corrections to ensure that the generator has the full period
void round(MT128 *a, MT128 *b, MT128 *c, MT128 *d); // a = MTMIX(a,b,c,d)
void update(); // permute the existing state into a new one
public:
MersenneTwister();
bool Initialize(u32 seed);
bool Initialize(u32 *seeds, u32 words);
bool Initialize();
u32 Generate(); // generate a 32-bit number
void Generate(void *buffer, int bytes); // generate a series of random numbers
// Generate a 32-bit random number in the range [low..high] inclusive
u32 GenerateUnbiased(u32 low, u32 high)
{
u32 range = high - low;
if (range == 0) return low;
// Round range up to the next pow(2)-1
u32 available = BSR32(range);
u32 v = (((1 << available) - 1) << 1) | 1;
available = 32 - available;
// Generate an unbiased random number in the inclusive range [0..(high-low)]
u32 x;
for (u32 trials = 0; trials < 16; ++trials)
{
x = Generate();
u32 y = x;
u32 count = available;
while (count--)
{
u32 z = y & v;
if (z <= range) return low + z;
y >>= 1;
}
}
// Give up on being unbiased because it is taking too long
return low + (x % (range + 1));
}
// Uniform float: [0, 1]
float Uni() {
/* ******************************************************************
Generates a random floating-point number between -1.0 and
1.0. Not sure if this is the best precision possible, but is
based on bitwise operations, and I believe it should be faster
than using (float)rand()/RAND_MAX.
We pick up the number generated by rand, and leave just the bits
over the mantissa region. The signal and exponent part are
replaced to generate a number between 2.0 and 3.9999999. We
then subtract 3.0 to bring the range down to -1.0 and 0.9999999.
Coded in 2010-09-24 by Nicolau Werneck <[email protected]>.
*************************************************************** */
static union {
u32 i;
f32 f;
} num;
num.i = (Generate() & 0x007fffff) | 0x40000000;
return (num.f - 2.f) / 2.f;
}
// Call these functions before the following (not thread-safe)
static void InitializeNor();
static void InitializeExp();
// X~StdNormal: Mean = 0, Var = 1, aX + b -> mean = b, var = a*a
float Nor();
// X~StdExp: Mean = 1, kX ~ Exp(1/k)
float Exp();
};
} // namespace cat
#endif // CAT_MERSENNE_TWISTER_HPP