... | ... |
@@ -20,98 +20,97 @@ |
20 | 20 |
* This file contains the reimplemented version of the Mersenne Twister |
21 | 21 |
* Generator of Matsumoto and Nishimura. |
22 | 22 |
* |
23 | 23 |
* See the appropriate copyright notice below. |
24 | 24 |
* |
25 | 25 |
* Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, |
26 | 26 |
* All rights reserved. |
27 | 27 |
* |
28 | 28 |
* Redistribution and use in source and binary forms, with or without |
29 | 29 |
* modification, are permitted provided that the following conditions |
30 | 30 |
* are met: |
31 | 31 |
* |
32 | 32 |
* 1. Redistributions of source code must retain the above copyright |
33 | 33 |
* notice, this list of conditions and the following disclaimer. |
34 | 34 |
* |
35 | 35 |
* 2. Redistributions in binary form must reproduce the above copyright |
36 | 36 |
* notice, this list of conditions and the following disclaimer in the |
37 | 37 |
* documentation and/or other materials provided with the distribution. |
38 | 38 |
* |
39 | 39 |
* 3. The names of its contributors may not be used to endorse or promote |
40 | 40 |
* products derived from this software without specific prior written |
41 | 41 |
* permission. |
42 | 42 |
* |
43 | 43 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
44 | 44 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
45 | 45 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
46 | 46 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
47 | 47 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
48 | 48 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
49 | 49 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
50 | 50 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
51 | 51 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
52 | 52 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | 53 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
54 | 54 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
55 | 55 |
* |
56 | 56 |
* |
57 | 57 |
* Any feedback is very welcome. |
58 | 58 |
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html |
59 | 59 |
* email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) |
60 | 60 |
*/ |
61 | 61 |
|
62 | 62 |
#ifndef LEMON_RANDOM_H |
63 | 63 |
#define LEMON_RANDOM_H |
64 | 64 |
|
65 | 65 |
#include <algorithm> |
66 | 66 |
#include <iterator> |
67 | 67 |
#include <vector> |
68 |
|
|
69 |
#include <ctime> |
|
68 |
#include <limits> |
|
70 | 69 |
|
71 | 70 |
#include <lemon/math.h> |
72 | 71 |
#include <lemon/dim2.h> |
73 | 72 |
|
74 | 73 |
///\ingroup misc |
75 | 74 |
///\file |
76 | 75 |
///\brief Mersenne Twister random number generator |
77 | 76 |
|
78 | 77 |
namespace lemon { |
79 | 78 |
|
80 | 79 |
namespace _random_bits { |
81 | 80 |
|
82 | 81 |
template <typename _Word, int _bits = std::numeric_limits<_Word>::digits> |
83 | 82 |
struct RandomTraits {}; |
84 | 83 |
|
85 | 84 |
template <typename _Word> |
86 | 85 |
struct RandomTraits<_Word, 32> { |
87 | 86 |
|
88 | 87 |
typedef _Word Word; |
89 | 88 |
static const int bits = 32; |
90 | 89 |
|
91 | 90 |
static const int length = 624; |
92 | 91 |
static const int shift = 397; |
93 | 92 |
|
94 | 93 |
static const Word mul = 0x6c078965u; |
95 | 94 |
static const Word arrayInit = 0x012BD6AAu; |
96 | 95 |
static const Word arrayMul1 = 0x0019660Du; |
97 | 96 |
static const Word arrayMul2 = 0x5D588B65u; |
98 | 97 |
|
99 | 98 |
static const Word mask = 0x9908B0DFu; |
100 | 99 |
static const Word loMask = (1u << 31) - 1; |
101 | 100 |
static const Word hiMask = ~loMask; |
102 | 101 |
|
103 | 102 |
|
104 | 103 |
static Word tempering(Word rnd) { |
105 | 104 |
rnd ^= (rnd >> 11); |
106 | 105 |
rnd ^= (rnd << 7) & 0x9D2C5680u; |
107 | 106 |
rnd ^= (rnd << 15) & 0xEFC60000u; |
108 | 107 |
rnd ^= (rnd >> 18); |
109 | 108 |
return rnd; |
110 | 109 |
} |
111 | 110 |
|
112 | 111 |
}; |
113 | 112 |
|
114 | 113 |
template <typename _Word> |
115 | 114 |
struct RandomTraits<_Word, 64> { |
116 | 115 |
|
117 | 116 |
typedef _Word Word; |
0 comments (0 inline)