klee
RNG.cpp
Go to the documentation of this file.
1/*
2 A C-program for MT19937, with initialization improved 2002/1/26.
3 Coded by Takuji Nishimura and Makoto Matsumoto.
4 Modified to be a C++ class by Daniel Dunbar.
5
6 Before using, initialize the state by using init_genrand(seed)
7 or init_by_array(init_key, key_length).
8
9 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
10 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions
14 are met:
15
16 1. Redistributions of source code must retain the above copyright
17 notice, this list of conditions and the following disclaimer.
18
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22
23 3. The names of its contributors may not be used to endorse or promote
24 products derived from this software without specific prior written
25 permission.
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39
40 Any feedback is very welcome.
41 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
42 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
43*/
44
45#include "klee/ADT/RNG.h"
47
48using namespace klee;
49
50namespace {
51llvm::cl::opt<unsigned> RNGInitialSeed(
52 "rng-initial-seed", llvm::cl::init(5489U),
53 llvm::cl::desc("seed value for random number generator (default=5489)"),
54 llvm::cl::cat(klee::MiscCat));
55
56}
57
58RNG::RNG() {
59 seed(RNGInitialSeed);
60}
61
62/* initializes mt[N] with a seed */
63RNG::RNG(unsigned int s) {
64 seed(s);
65}
66
67void RNG::seed(unsigned int s) {
68 mt[0]= s & 0xffffffffUL;
69 for (mti=1; mti<N; mti++) {
70 mt[mti] =
71 (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30U)) + mti);
72 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
73 /* In the previous versions, MSBs of the seed affect */
74 /* only MSBs of the array mt[]. */
75 /* 2002/01/09 modified by Makoto Matsumoto */
76 mt[mti] &= 0xffffffffUL;
77 /* for >32 bit machines */
78 }
79}
80
81/* generates a random number on [0,0xffffffff]-interval */
82unsigned int RNG::getInt32() {
83 unsigned int y;
84 static unsigned int mag01[2]={0x0UL, MATRIX_A};
85 /* mag01[x] = x * MATRIX_A for x=0,1 */
86
87 if (mti >= N) { /* generate N words at one time */
88 int kk;
89
90 for (kk=0;kk<N-M;kk++) {
91 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
92 mt[kk] = mt[kk+M] ^ (y >> 1U) ^ mag01[y & 0x1UL];
93 }
94 for (;kk<N-1;kk++) {
95 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
96 mt[kk] = mt[kk+(M-N)] ^ (y >> 1U) ^ mag01[y & 0x1UL];
97 }
98 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
99 mt[N-1] = mt[M-1] ^ (y >> 1U) ^ mag01[y & 0x1UL];
100
101 mti = 0;
102 }
103
104 y = mt[mti++];
105
106 /* Tempering */
107 y ^= (y >> 11U);
108 y ^= (y << 7U) & 0x9d2c5680UL;
109 y ^= (y << 15U) & 0xefc60000UL;
110 y ^= (y >> 18U);
111
112 return y;
113}
114
115/* generates a random number on [0,0x7fffffff]-interval */
117 return (int)(getInt32() >> 1U);
118}
119
120/* generates a random number on [0,1]-real-interval */
122 return getInt32()*(1.0/4294967295.0);
123 /* divided by 2^32-1 */
124}
125
126/* generates a random number on [0,1)-real-interval */
128 return getInt32()*(1.0/4294967296.0);
129 /* divided by 2^32 */
130}
131
132/* generates a random number on (0,1)-real-interval */
134 return (((double)getInt32()) + 0.5)*(1.0/4294967296.0);
135 /* divided by 2^32 */
136}
137
139 return getInt32()*(1.0f/4294967295.0f);
140 /* divided by 2^32-1 */
141}
143 return getInt32()*(1.0f/4294967296.0f);
144 /* divided by 2^32 */
145}
147 return (getInt32() + 0.5f)*(1.0f/4294967296.0f);
148 /* divided by 2^32 */
149}
150
152 unsigned bits = getInt32();
153 bits ^= bits >> 16U;
154 bits ^= bits >> 8U;
155 bits ^= bits >> 4U;
156 bits ^= bits >> 2U;
157 bits ^= bits >> 1U;
158 return bits & 1U;
159}
RNG()
Definition: RNG.cpp:58
double getDoubleL()
Definition: RNG.cpp:127
double getDouble()
Definition: RNG.cpp:133
static const unsigned int LOWER_MASK
Definition: RNG.h:21
unsigned int getInt32()
Definition: RNG.cpp:82
void seed(unsigned int seed)
Definition: RNG.cpp:67
int mti
Definition: RNG.h:25
static const unsigned int UPPER_MASK
Definition: RNG.h:20
static const int N
Definition: RNG.h:17
float getFloatL()
Definition: RNG.cpp:142
double getDoubleLR()
Definition: RNG.cpp:121
static const int M
Definition: RNG.h:18
unsigned int mt[N]
Definition: RNG.h:24
int getInt31()
Definition: RNG.cpp:116
float getFloatLR()
Definition: RNG.cpp:138
float getFloat()
Definition: RNG.cpp:146
bool getBool()
Definition: RNG.cpp:151
static const unsigned int MATRIX_A
Definition: RNG.h:19
Definition: main.cpp:291
llvm::cl::OptionCategory MiscCat