2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
33 * Mersenne Twister random number generator has a period of
36 * The actual math is in its own file to keep the license clear.
39 #ifndef __BASE_RANDOM_HH__
40 #define __BASE_RANDOM_HH__
45 #include "base/range.hh"
46 #include "base/types.hh"
53 static const int N = 624;
54 static const int M = 397;
55 static const uint32_t MATRIX_A = (uint32_t)0x9908b0df;
56 static const uint32_t UPPER_MASK = (uint32_t)0x80000000;
57 static const uint32_t LOWER_MASK = (uint32_t)0x7fffffff;
63 uint32_t genrand(uint32_t max);
64 uint64_t genrand(uint64_t max);
67 _random(int8_t &value)
69 value = genrand() & (int8_t)-1;
73 _random(int16_t &value)
75 value = genrand() & (int16_t)-1;
79 _random(int32_t &value)
81 value = (int32_t)genrand();
85 _random(int64_t &value)
87 value = (int64_t)genrand() << 32 | (int64_t)genrand();
91 _random(uint8_t &value)
93 value = genrand() & (uint8_t)-1;
97 _random(uint16_t &value)
99 value = genrand() & (uint16_t)-1;
103 _random(uint32_t &value)
109 _random(uint64_t &value)
111 value = (uint64_t)genrand() << 32 | (uint64_t)genrand();
116 _random(float &value)
118 // ieee floats have 23 bits of mantissa
119 value = (genrand() >> 9) / 8388608.0;
124 _random(double &value)
126 double number = genrand() * 2097152.0 + (genrand() >> 11);
127 value = number / 9007199254740992.0;
131 // Range based versions of the random number generator
133 _random(int8_t min, int8_t max)
135 uint32_t diff = max - min;
136 return static_cast<int8_t>(min + genrand(diff));
140 _random(int16_t min, int16_t max)
142 uint32_t diff = max - min;
143 return static_cast<int16_t>(min + genrand(diff));
147 _random(int32_t min, int32_t max)
149 uint32_t diff = max - min;
150 return static_cast<int32_t>(min + genrand(diff));
154 _random(int64_t min, int64_t max)
156 uint64_t diff = max - min;
157 return static_cast<int64_t>(min + genrand(diff));
161 _random(uint8_t min, uint8_t max)
163 uint32_t diff = max - min;
164 return static_cast<uint8_t>(min + genrand(diff));
168 _random(uint16_t min, uint16_t max)
170 uint32_t diff = max - min;
171 return static_cast<uint16_t>(min + genrand(diff));
175 _random(uint32_t min, uint32_t max)
177 uint32_t diff = max - min;
178 return static_cast<uint32_t>(min + genrand(diff));
182 _random(uint64_t min, uint64_t max)
184 uint64_t diff = max - min;
185 return static_cast<uint64_t>(min + genrand(diff));
191 Random(uint32_t init_key[], int key_length);
194 void init(uint32_t s);
195 void init(uint32_t init_key[], int key_length);
197 template <typename T>
206 template <typename T>
210 return _random(min, max);
213 template <typename T>
215 random(const Range<T> &range)
217 return _random(range.start, range.end);
224 return genrand() / 4294967296.0;
231 return genrand() / 4294967295.0;
238 return ((double)genrand() + 0.5) / 4294967296.0;
242 void serialize(const std::string &base, std::ostream &os);
243 void unserialize(const std::string &base, Checkpoint *cp,
244 const std::string §ion);
247 extern Random random_mt;
249 #endif // __BASE_RANDOM_HH__