random.hh revision 7771:e10aff32c561
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
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 &section);
245};
246
247extern Random random_mt;
248
249#endif // __BASE_RANDOM_HH__
250