2 //Mersenne Twister pseudorandom number generator of 32 and 64-bit.
\r
3 //2005, Diego Park <diegopark@gmail.com>
\r
5 //A C-program for MT19937, with initialization improved 2002/1/26.
\r
6 //Coded by Takuji Nishimura and Makoto Matsumoto.
\r
8 //Before using, initialize the state by using init_genrand(seed)
\r
9 //or init_by_array(init_key, key_length).
\r
11 //Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
\r
12 //All rights reserved.
\r
14 //A C-program for MT19937-64 (2004/9/29 version).
\r
15 //Coded by Takuji Nishimura and Makoto Matsumoto.
\r
17 //This is a 64-bit version of Mersenne Twister pseudorandom number
\r
20 //Before using, initialize the state by using init_genrand64(seed)
\r
21 //or init_by_array64(init_key, key_length).
\r
23 //Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
\r
24 //All rights reserved.
\r
26 //Redistribution and use in source and binary forms, with or without
\r
27 //modification, are permitted provided that the following conditions
\r
30 //1. Redistributions of source code must retain the above copyright
\r
31 //notice, this list of conditions and the following disclaimer.
\r
33 //2. Redistributions in binary form must reproduce the above copyright
\r
34 //notice, this list of conditions and the following disclaimer in the
\r
35 //documentation and/or other materials provided with the distribution.
\r
37 //3. The names of its contributors may not be used to endorse or promote
\r
38 //products derived from this software without specific prior written
\r
41 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\r
42 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
\r
43 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
\r
44 //A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
\r
45 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
46 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
\r
47 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
\r
48 //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
49 //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
50 //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
\r
51 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
54 //T. Nishimura, ``Tables of 64-bit Mersenne Twisters''
\r
55 //ACM Transactions on Modeling and
\r
56 //Computer Simulation 10. (2000) 348--357.
\r
57 //M. Matsumoto and T. Nishimura,
\r
58 //``Mersenne Twister: a 623-dimensionally equidistributed
\r
59 //uniform pseudorandom number generator''
\r
60 //ACM Transactions on Modeling and
\r
61 //Computer Simulation 8. (Jan. 1998) 3--30.
\r
63 //Any feedback is very welcome.
\r
64 //http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html
\r
65 //email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces)
\r
67 template <class Integer>
\r
68 Integer Random<Integer>::next() {
\r
69 if (index >= MaxState) {
\r
73 return hashFunction(state[index++]);
\r
76 template <class Integer>
\r
77 void Random<Integer>::seed() {
\r
81 for (i = 0; i < MaxState - Period; i++) {
\r
82 state[i] = twist(state[i + Period], state[i], state[i + 1]);
\r
84 for (; i < MaxState - 1; i++) {
\r
85 state[i] = twist(state[i + (Period - MaxState)], state[i], state[i + 1]);
\r
87 state[MaxState - 1] = twist(state[Period - 1], state[MaxState - 1], state[0]);
\r