random.hh (7771:e10aff32c561) | random.hh (9235:5aa4896ed55a) |
---|---|
1/* 2 * Copyright (c) 2003-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 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. 15 * 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. 27 * 28 * Authors: Nathan Binkert 29 * Ali Saidi 30 */ 31 32/* 33 * Mersenne Twister random number generator has a period of 34 * 2^19937-1. 35 * 36 * The actual math is in its own file to keep the license clear. 37 */ 38 39#ifndef __BASE_RANDOM_HH__ 40#define __BASE_RANDOM_HH__ 41 42#include <ios> 43#include <string> 44 | 1/* 2 * Copyright (c) 2003-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 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. 15 * 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. 27 * 28 * Authors: Nathan Binkert 29 * Ali Saidi 30 */ 31 32/* 33 * Mersenne Twister random number generator has a period of 34 * 2^19937-1. 35 * 36 * The actual math is in its own file to keep the license clear. 37 */ 38 39#ifndef __BASE_RANDOM_HH__ 40#define __BASE_RANDOM_HH__ 41 42#include <ios> 43#include <string> 44 |
45#include "base/range.hh" | |
46#include "base/types.hh" 47 48class Checkpoint; 49 50class Random 51{ 52 protected: 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; 58 59 uint32_t mt[N]; 60 int mti; 61 62 uint32_t genrand(); 63 uint32_t genrand(uint32_t max); 64 uint64_t genrand(uint64_t max); 65 66 void 67 _random(int8_t &value) 68 { 69 value = genrand() & (int8_t)-1; 70 } 71 72 void 73 _random(int16_t &value) 74 { 75 value = genrand() & (int16_t)-1; 76 } 77 78 void 79 _random(int32_t &value) 80 { 81 value = (int32_t)genrand(); 82 } 83 84 void 85 _random(int64_t &value) 86 { 87 value = (int64_t)genrand() << 32 | (int64_t)genrand(); 88 } 89 90 void 91 _random(uint8_t &value) 92 { 93 value = genrand() & (uint8_t)-1; 94 } 95 96 void 97 _random(uint16_t &value) 98 { 99 value = genrand() & (uint16_t)-1; 100 } 101 102 void 103 _random(uint32_t &value) 104 { 105 value = genrand(); 106 } 107 108 void 109 _random(uint64_t &value) 110 { 111 value = (uint64_t)genrand() << 32 | (uint64_t)genrand(); 112 } 113 114 // [0,1] 115 void 116 _random(float &value) 117 { 118 // ieee floats have 23 bits of mantissa 119 value = (genrand() >> 9) / 8388608.0; 120 } 121 122 // [0,1] 123 void 124 _random(double &value) 125 { 126 double number = genrand() * 2097152.0 + (genrand() >> 11); 127 value = number / 9007199254740992.0; 128 } 129 130 131 // Range based versions of the random number generator 132 int8_t 133 _random(int8_t min, int8_t max) 134 { 135 uint32_t diff = max - min; 136 return static_cast<int8_t>(min + genrand(diff)); 137 } 138 139 int16_t 140 _random(int16_t min, int16_t max) 141 { 142 uint32_t diff = max - min; 143 return static_cast<int16_t>(min + genrand(diff)); 144 } 145 146 int32_t 147 _random(int32_t min, int32_t max) 148 { 149 uint32_t diff = max - min; 150 return static_cast<int32_t>(min + genrand(diff)); 151 } 152 153 int64_t 154 _random(int64_t min, int64_t max) 155 { 156 uint64_t diff = max - min; 157 return static_cast<int64_t>(min + genrand(diff)); 158 } 159 160 uint8_t 161 _random(uint8_t min, uint8_t max) 162 { 163 uint32_t diff = max - min; 164 return static_cast<uint8_t>(min + genrand(diff)); 165 } 166 167 uint16_t 168 _random(uint16_t min, uint16_t max) 169 { 170 uint32_t diff = max - min; 171 return static_cast<uint16_t>(min + genrand(diff)); 172 } 173 174 uint32_t 175 _random(uint32_t min, uint32_t max) 176 { 177 uint32_t diff = max - min; 178 return static_cast<uint32_t>(min + genrand(diff)); 179 } 180 181 uint64_t 182 _random(uint64_t min, uint64_t max) 183 { 184 uint64_t diff = max - min; 185 return static_cast<uint64_t>(min + genrand(diff)); 186 } 187 188 public: 189 Random(); 190 Random(uint32_t s); 191 Random(uint32_t init_key[], int key_length); 192 ~Random(); 193 194 void init(uint32_t s); 195 void init(uint32_t init_key[], int key_length); 196 197 template <typename T> 198 T 199 random() 200 { 201 T value; 202 _random(value); 203 return value; 204 } 205 206 template <typename T> 207 T 208 random(T min, T max) 209 { 210 return _random(min, max); 211 } 212 | 45#include "base/types.hh" 46 47class Checkpoint; 48 49class Random 50{ 51 protected: 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; 57 58 uint32_t mt[N]; 59 int mti; 60 61 uint32_t genrand(); 62 uint32_t genrand(uint32_t max); 63 uint64_t genrand(uint64_t max); 64 65 void 66 _random(int8_t &value) 67 { 68 value = genrand() & (int8_t)-1; 69 } 70 71 void 72 _random(int16_t &value) 73 { 74 value = genrand() & (int16_t)-1; 75 } 76 77 void 78 _random(int32_t &value) 79 { 80 value = (int32_t)genrand(); 81 } 82 83 void 84 _random(int64_t &value) 85 { 86 value = (int64_t)genrand() << 32 | (int64_t)genrand(); 87 } 88 89 void 90 _random(uint8_t &value) 91 { 92 value = genrand() & (uint8_t)-1; 93 } 94 95 void 96 _random(uint16_t &value) 97 { 98 value = genrand() & (uint16_t)-1; 99 } 100 101 void 102 _random(uint32_t &value) 103 { 104 value = genrand(); 105 } 106 107 void 108 _random(uint64_t &value) 109 { 110 value = (uint64_t)genrand() << 32 | (uint64_t)genrand(); 111 } 112 113 // [0,1] 114 void 115 _random(float &value) 116 { 117 // ieee floats have 23 bits of mantissa 118 value = (genrand() >> 9) / 8388608.0; 119 } 120 121 // [0,1] 122 void 123 _random(double &value) 124 { 125 double number = genrand() * 2097152.0 + (genrand() >> 11); 126 value = number / 9007199254740992.0; 127 } 128 129 130 // Range based versions of the random number generator 131 int8_t 132 _random(int8_t min, int8_t max) 133 { 134 uint32_t diff = max - min; 135 return static_cast<int8_t>(min + genrand(diff)); 136 } 137 138 int16_t 139 _random(int16_t min, int16_t max) 140 { 141 uint32_t diff = max - min; 142 return static_cast<int16_t>(min + genrand(diff)); 143 } 144 145 int32_t 146 _random(int32_t min, int32_t max) 147 { 148 uint32_t diff = max - min; 149 return static_cast<int32_t>(min + genrand(diff)); 150 } 151 152 int64_t 153 _random(int64_t min, int64_t max) 154 { 155 uint64_t diff = max - min; 156 return static_cast<int64_t>(min + genrand(diff)); 157 } 158 159 uint8_t 160 _random(uint8_t min, uint8_t max) 161 { 162 uint32_t diff = max - min; 163 return static_cast<uint8_t>(min + genrand(diff)); 164 } 165 166 uint16_t 167 _random(uint16_t min, uint16_t max) 168 { 169 uint32_t diff = max - min; 170 return static_cast<uint16_t>(min + genrand(diff)); 171 } 172 173 uint32_t 174 _random(uint32_t min, uint32_t max) 175 { 176 uint32_t diff = max - min; 177 return static_cast<uint32_t>(min + genrand(diff)); 178 } 179 180 uint64_t 181 _random(uint64_t min, uint64_t max) 182 { 183 uint64_t diff = max - min; 184 return static_cast<uint64_t>(min + genrand(diff)); 185 } 186 187 public: 188 Random(); 189 Random(uint32_t s); 190 Random(uint32_t init_key[], int key_length); 191 ~Random(); 192 193 void init(uint32_t s); 194 void init(uint32_t init_key[], int key_length); 195 196 template <typename T> 197 T 198 random() 199 { 200 T value; 201 _random(value); 202 return value; 203 } 204 205 template <typename T> 206 T 207 random(T min, T max) 208 { 209 return _random(min, max); 210 } 211 |
213 template <typename T> 214 T 215 random(const Range<T> &range) 216 { 217 return _random(range.start, range.end); 218 } 219 | |
220 // [0,1] 221 double 222 gen_real1() 223 { 224 return genrand() / 4294967296.0; 225 } 226 227 // [0,1) 228 double 229 gen_real2() 230 { 231 return genrand() / 4294967295.0; 232 } 233 234 // (0,1) 235 double 236 gen_real3() 237 { 238 return ((double)genrand() + 0.5) / 4294967296.0; 239 } 240 241 public: 242 void serialize(const std::string &base, std::ostream &os); 243 void unserialize(const std::string &base, Checkpoint *cp, 244 const std::string §ion); 245}; 246 247extern Random random_mt; 248 249#endif // __BASE_RANDOM_HH__ | 212 // [0,1] 213 double 214 gen_real1() 215 { 216 return genrand() / 4294967296.0; 217 } 218 219 // [0,1) 220 double 221 gen_real2() 222 { 223 return genrand() / 4294967295.0; 224 } 225 226 // (0,1) 227 double 228 gen_real3() 229 { 230 return ((double)genrand() + 0.5) / 4294967296.0; 231 } 232 233 public: 234 void serialize(const std::string &base, std::ostream &os); 235 void unserialize(const std::string &base, Checkpoint *cp, 236 const std::string §ion); 237}; 238 239extern Random random_mt; 240 241#endif // __BASE_RANDOM_HH__ |