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/types.hh"
52 static const int N = 624;
53 static const int M = 397;
54 static const uint32_t MATRIX_A = (uint32_t)0x9908b0df;
55 static const uint32_t UPPER_MASK = (uint32_t)0x80000000;
56 static const uint32_t LOWER_MASK = (uint32_t)0x7fffffff;
62 uint32_t genrand(uint32_t max);
63 uint64_t genrand(uint64_t max);
66 _random(int8_t &value)
68 value = genrand() & (int8_t)-1;
72 _random(int16_t &value)
74 value = genrand() & (int16_t)-1;
78 _random(int32_t &value)
80 value = (int32_t)genrand();
84 _random(int64_t &value)
86 value = (int64_t)genrand() << 32 | (int64_t)genrand();
90 _random(uint8_t &value)
92 value = genrand() & (uint8_t)-1;
96 _random(uint16_t &value)
98 value = genrand() & (uint16_t)-1;
102 _random(uint32_t &value)
108 _random(uint64_t &value)
110 value = (uint64_t)genrand() << 32 | (uint64_t)genrand();
115 _random(float &value)
117 // ieee floats have 23 bits of mantissa
118 value = (genrand() >> 9) / 8388608.0;
123 _random(double &value)
125 double number = genrand() * 2097152.0 + (genrand() >> 11);
126 value = number / 9007199254740992.0;
130 // Range based versions of the random number generator
132 _random(int8_t min, int8_t max)
134 uint32_t diff = max - min;
135 return static_cast<int8_t>(min + genrand(diff));
139 _random(int16_t min, int16_t max)
141 uint32_t diff = max - min;
142 return static_cast<int16_t>(min + genrand(diff));
146 _random(int32_t min, int32_t max)
148 uint32_t diff = max - min;
149 return static_cast<int32_t>(min + genrand(diff));
153 _random(int64_t min, int64_t max)
155 uint64_t diff = max - min;
156 return static_cast<int64_t>(min + genrand(diff));
160 _random(uint8_t min, uint8_t max)
162 uint32_t diff = max - min;
163 return static_cast<uint8_t>(min + genrand(diff));
167 _random(uint16_t min, uint16_t max)
169 uint32_t diff = max - min;
170 return static_cast<uint16_t>(min + genrand(diff));
174 _random(uint32_t min, uint32_t max)
176 uint32_t diff = max - min;
177 return static_cast<uint32_t>(min + genrand(diff));
181 _random(uint64_t min, uint64_t max)
183 uint64_t diff = max - min;
184 return static_cast<uint64_t>(min + genrand(diff));
190 Random(uint32_t init_key[], int key_length);
193 void init(uint32_t s);
194 void init(uint32_t init_key[], int key_length);
196 template <typename T>
205 template <typename T>
209 return _random(min, max);
216 return genrand() / 4294967296.0;
223 return genrand() / 4294967295.0;
230 return ((double)genrand() + 0.5) / 4294967296.0;
234 void serialize(const std::string &base, std::ostream &os);
235 void unserialize(const std::string &base, Checkpoint *cp,
236 const std::string §ion);
239 extern Random random_mt;
241 #endif // __BASE_RANDOM_HH__