annotate MersenneTwister.h @ 1:f63d649f91b4 default tip

fix include path
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Mon, 19 May 2008 10:23:11 +0200
parents 0fa810263337
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 // MersenneTwister.h
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2 // Mersenne Twister random number generator -- a C++ class MTRand
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3 // Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4 // Richard J. Wagner v1.0 15 May 2003 rjwagner@writeme.com
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6 // The Mersenne Twister is an algorithm for generating random numbers. It
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 // was designed with consideration of the flaws in various other generators.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 // The period, 2^19937-1, and the order of equidistribution, 623 dimensions,
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 // are far greater. The generator is also fast; it avoids multiplication and
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 // division, and it benefits from caches and pipelines. For more information
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 // see the inventors' web page at http://www.math.keio.ac.jp/~matumoto/emt.html
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 // Reference
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 // M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 // Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions on
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 // Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 // Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 // Copyright (C) 2000 - 2003, Richard J. Wagner
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 // All rights reserved.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 // Redistribution and use in source and binary forms, with or without
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23 // modification, are permitted provided that the following conditions
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24 // are met:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 // 1. Redistributions of source code must retain the above copyright
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 // notice, this list of conditions and the following disclaimer.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 // 2. Redistributions in binary form must reproduce the above copyright
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30 // notice, this list of conditions and the following disclaimer in the
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 // documentation and/or other materials provided with the distribution.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33 // 3. The names of its contributors may not be used to endorse or promote
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 // products derived from this software without specific prior written
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35 // permission.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49 // The original code included the following notice:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51 // When you use this, send an email to: matumoto@math.keio.ac.jp
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52 // with an appropriate reference to your work.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 // It would be nice to CC: rjwagner@writeme.com and Cokus@math.washington.edu
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55 // when you write.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57 #ifndef MERSENNETWISTER_H
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58 #define MERSENNETWISTER_H
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 // Not thread safe (unless auto-initialization is avoided and each thread has
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61 // its own MTRand object)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63 #include <iostream>
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64 #include <limits.h>
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65 #include <stdio.h>
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 #include <time.h>
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67 #include <math.h>
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 class MTRand {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70 // Data
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
71 public:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72 typedef unsigned long uint32; // unsigned integer type, at least 32 bits
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 enum { N = 624 }; // length of state vector
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 enum { SAVE = N + 1 }; // length of array for save()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77 protected:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78 enum { M = 397 }; // period parameter
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80 uint32 state[N]; // internal state
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81 uint32 *pNext; // next value to get from state
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 int left; // number of values left before reload needed
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85 //Methods
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86 public:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 MTRand( const uint32& oneSeed ); // initialize with a simple uint32
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 MTRand( uint32 *const bigSeed, uint32 const seedLength = N ); // or an array
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89 MTRand(); // auto-initialize with /dev/urandom or time() and clock()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 // Do NOT use for CRYPTOGRAPHY without securely hashing several returned
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 // values together, otherwise the generator state can be learned after
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93 // reading 624 consecutive values.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95 // Access to 32-bit random numbers
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96 double rand(); // real number in [0,1]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 double rand( const double& n ); // real number in [0,n]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98 double randExc(); // real number in [0,1)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99 double randExc( const double& n ); // real number in [0,n)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100 double randDblExc(); // real number in (0,1)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 double randDblExc( const double& n ); // real number in (0,n)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 uint32 randInt(); // integer in [0,2^32-1]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103 uint32 randInt( const uint32& n ); // integer in [0,n] for n < 2^32
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104 double operator()() { return rand(); } // same as rand()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 // Access to 53-bit random numbers (capacity of IEEE double precision)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107 double rand53(); // real number in [0,1)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109 // Access to nonuniform random number distributions
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110 double randNorm( const double& mean = 0.0, const double& variance = 0.0 );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
111
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112 // Re-seeding functions with same behavior as initializers
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113 void seed( const uint32 oneSeed );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 void seed( uint32 *const bigSeed, const uint32 seedLength = N );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115 void seed();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
117 // Saving and loading generator state
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118 void save( uint32* saveArray ) const; // to array of size SAVE
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119 void load( uint32 *const loadArray ); // from such array
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120 friend std::ostream& operator<<( std::ostream& os, const MTRand& mtrand );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
121 friend std::istream& operator>>( std::istream& is, MTRand& mtrand );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123 protected:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124 void initialize( const uint32 oneSeed );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 void reload();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126 uint32 hiBit( const uint32& u ) const { return u & 0x80000000UL; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127 uint32 loBit( const uint32& u ) const { return u & 0x00000001UL; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 uint32 loBits( const uint32& u ) const { return u & 0x7fffffffUL; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129 uint32 mixBits( const uint32& u, const uint32& v ) const
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130 { return hiBit(u) | loBits(v); }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131 uint32 twist( const uint32& m, const uint32& s0, const uint32& s1 ) const
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 { return m ^ (mixBits(s0,s1)>>1) ^ (-loBit(s1) & 0x9908b0dfUL); }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133 static uint32 hash( time_t t, clock_t c );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134 };
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
136
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 inline MTRand::MTRand( const uint32& oneSeed )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138 { seed(oneSeed); }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140 inline MTRand::MTRand( uint32 *const bigSeed, const uint32 seedLength )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141 { seed(bigSeed,seedLength); }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143 inline MTRand::MTRand()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
144 { seed(); }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
145
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
146 inline double MTRand::rand()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
147 { return double(randInt()) * (1.0/4294967295.0); }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
148
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
149 inline double MTRand::rand( const double& n )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
150 { return rand() * n; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
151
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
152 inline double MTRand::randExc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
153 { return double(randInt()) * (1.0/4294967296.0); }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
154
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
155 inline double MTRand::randExc( const double& n )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
156 { return randExc() * n; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
157
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
158 inline double MTRand::randDblExc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
159 { return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
160
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
161 inline double MTRand::randDblExc( const double& n )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
162 { return randDblExc() * n; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
163
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
164 inline double MTRand::rand53()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
165 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
166 uint32 a = randInt() >> 5, b = randInt() >> 6;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
167 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0); // by Isaku Wada
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
168 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
169
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
170 inline double MTRand::randNorm( const double& mean, const double& variance )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
171 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
172 // Return a real number from a normal (Gaussian) distribution with given
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
173 // mean and variance by Box-Muller method
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
174 double r = sqrt( -2.0 * log( 1.0-randDblExc()) ) * variance;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
175 double phi = 2.0 * 3.14159265358979323846264338328 * randExc();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
176 return mean + r * cos(phi);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
177 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
178
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
179 inline MTRand::uint32 MTRand::randInt()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
180 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
181 // Pull a 32-bit integer from the generator state
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
182 // Every other access function simply transforms the numbers extracted here
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
183
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
184 if( left == 0 ) reload();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
185 --left;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
186
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
187 register uint32 s1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
188 s1 = *pNext++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
189 s1 ^= (s1 >> 11);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
190 s1 ^= (s1 << 7) & 0x9d2c5680UL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
191 s1 ^= (s1 << 15) & 0xefc60000UL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
192 return ( s1 ^ (s1 >> 18) );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
193 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
194
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
195 inline MTRand::uint32 MTRand::randInt( const uint32& n )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
196 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
197 // Find which bits are used in n
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
198 // Optimized by Magnus Jonsson (magnus@smartelectronix.com)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
199 uint32 used = n;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
200 used |= used >> 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
201 used |= used >> 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
202 used |= used >> 4;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
203 used |= used >> 8;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
204 used |= used >> 16;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
205
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
206 // Draw numbers until one is found in [0,n]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
207 uint32 i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
208 do
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
209 i = randInt() & used; // toss unused bits to shorten search
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
210 while( i > n );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
211 return i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
212 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
213
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
214
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
215 inline void MTRand::seed( const uint32 oneSeed )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
216 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
217 // Seed the generator with a simple uint32
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
218 initialize(oneSeed);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
219 reload();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
220 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
221
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
222
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
223 inline void MTRand::seed( uint32 *const bigSeed, const uint32 seedLength )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
224 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
225 // Seed the generator with an array of uint32's
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
226 // There are 2^19937-1 possible initial states. This function allows
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
227 // all of those to be accessed by providing at least 19937 bits (with a
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
228 // default seed length of N = 624 uint32's). Any bits above the lower 32
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
229 // in each element are discarded.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
230 // Just call seed() if you want to get array from /dev/urandom
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
231 initialize(19650218UL);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
232 register int i = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
233 register uint32 j = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
234 register int k = ( N > seedLength ? N : seedLength );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
235 for( ; k; --k )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
236 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
237 state[i] =
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
238 state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
239 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
240 state[i] &= 0xffffffffUL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
241 ++i; ++j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
242 if( i >= N ) { state[0] = state[N-1]; i = 1; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
243 if( j >= seedLength ) j = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
244 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
245 for( k = N - 1; k; --k )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
246 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
247 state[i] =
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
248 state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
249 state[i] -= i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
250 state[i] &= 0xffffffffUL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
251 ++i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
252 if( i >= N ) { state[0] = state[N-1]; i = 1; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
253 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
254 state[0] = 0x80000000UL; // MSB is 1, assuring non-zero initial array
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
255 reload();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
256 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
257
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
258
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
259 inline void MTRand::seed()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
260 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
261 // Seed the generator with an array from /dev/urandom if available
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
262 // Otherwise use a hash of time() and clock() values
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
263
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
264 // First try getting an array from /dev/urandom
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
265 FILE* urandom = fopen( "/dev/urandom", "rb" );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
266 if( urandom )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
267 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
268 uint32 bigSeed[N];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
269 register uint32 *s = bigSeed;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
270 register int i = N;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
271 register bool success = true;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
272 while( success && i-- )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
273 success = fread( s++, sizeof(uint32), 1, urandom );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
274 fclose(urandom);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
275 if( success ) { seed( bigSeed, N ); return; }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
276 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
277
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
278 // Was not successful, so use time() and clock() instead
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
279 seed( hash( time(NULL), clock() ) );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
280 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
281
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
282
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
283 inline void MTRand::initialize( const uint32 seed )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
284 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
285 // Initialize generator state with seed
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
286 // See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
287 // In previous versions, most significant bits (MSBs) of the seed affect
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
288 // only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
289 register uint32 *s = state;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
290 register uint32 *r = state;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
291 register int i = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
292 *s++ = seed & 0xffffffffUL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
293 for( ; i < N; ++i )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
294 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
295 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
296 r++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
297 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
298 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
299
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
300
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
301 inline void MTRand::reload()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
302 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
303 // Generate N new values in state
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
304 // Made clearer and faster by Matthew Bellew (matthew.bellew@home.com)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
305 register uint32 *p = state;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
306 register int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
307 for( i = N - M; i--; ++p )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
308 *p = twist( p[M], p[0], p[1] );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
309 for( i = M; --i; ++p )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
310 *p = twist( p[M-N], p[0], p[1] );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
311 *p = twist( p[M-N], p[0], state[0] );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
312
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
313 left = N, pNext = state;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
314 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
315
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
316
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
317 inline MTRand::uint32 MTRand::hash( time_t t, clock_t c )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
318 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
319 // Get a uint32 from t and c
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
320 // Better than uint32(x) in case x is floating point in [0,1]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
321 // Based on code by Lawrence Kirby (fred@genesis.demon.co.uk)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
322
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
323 static uint32 differ = 0; // guarantee time-based seeds will change
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
324
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
325 uint32 h1 = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
326 unsigned char *p = (unsigned char *) &t;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
327 for( size_t i = 0; i < sizeof(t); ++i )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
328 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
329 h1 *= UCHAR_MAX + 2U;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
330 h1 += p[i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
331 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
332 uint32 h2 = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
333 p = (unsigned char *) &c;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
334 for( size_t j = 0; j < sizeof(c); ++j )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
335 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
336 h2 *= UCHAR_MAX + 2U;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
337 h2 += p[j];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
338 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
339 return ( h1 + differ++ ) ^ h2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
340 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
341
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
342
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
343 inline void MTRand::save( uint32* saveArray ) const
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
344 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
345 register uint32 *sa = saveArray;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
346 register const uint32 *s = state;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
347 register int i = N;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
348 for( ; i--; *sa++ = *s++ ) {}
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
349 *sa = left;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
350 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
351
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
352
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
353 inline void MTRand::load( uint32 *const loadArray )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
354 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
355 register uint32 *s = state;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
356 register uint32 *la = loadArray;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
357 register int i = N;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
358 for( ; i--; *s++ = *la++ ) {}
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
359 left = *la;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
360 pNext = &state[N-left];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
361 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
362
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
363
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
364 inline std::ostream& operator<<( std::ostream& os, const MTRand& mtrand )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
365 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
366 register const MTRand::uint32 *s = mtrand.state;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
367 register int i = mtrand.N;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
368 for( ; i--; os << *s++ << "\t" ) {}
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
369 return os << mtrand.left;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
370 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
371
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
372
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
373 inline std::istream& operator>>( std::istream& is, MTRand& mtrand )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
374 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
375 register MTRand::uint32 *s = mtrand.state;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
376 register int i = mtrand.N;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
377 for( ; i--; is >> *s++ ) {}
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
378 is >> mtrand.left;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
379 mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
380 return is;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
381 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
382
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
383 #endif // MERSENNETWISTER_H
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
384
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
385 // Change log:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
386 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
387 // v0.1 - First release on 15 May 2000
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
388 // - Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
389 // - Translated from C to C++
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
390 // - Made completely ANSI compliant
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
391 // - Designed convenient interface for initialization, seeding, and
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
392 // obtaining numbers in default or user-defined ranges
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
393 // - Added automatic seeding from /dev/urandom or time() and clock()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
394 // - Provided functions for saving and loading generator state
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
395 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
396 // v0.2 - Fixed bug which reloaded generator one step too late
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
397 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
398 // v0.3 - Switched to clearer, faster reload() code from Matthew Bellew
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
399 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
400 // v0.4 - Removed trailing newline in saved generator format to be consistent
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
401 // with output format of built-in types
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
402 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
403 // v0.5 - Improved portability by replacing static const int's with enum's and
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
404 // clarifying return values in seed(); suggested by Eric Heimburg
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
405 // - Removed MAXINT constant; use 0xffffffffUL instead
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
406 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
407 // v0.6 - Eliminated seed overflow when uint32 is larger than 32 bits
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
408 // - Changed integer [0,n] generator to give better uniformity
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
409 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
410 // v0.7 - Fixed operator precedence ambiguity in reload()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
411 // - Added access for real numbers in (0,1) and (0,n)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
412 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
413 // v0.8 - Included time.h header to properly support time_t and clock_t
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
414 //
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
415 // v1.0 - Revised seeding to match 26 Jan 2002 update of Nishimura and Matsumoto
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
416 // - Allowed for seeding with arrays of any length
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
417 // - Added access for real numbers in [0,1) with 53-bit resolution
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
418 // - Added access for real numbers from normal (Gaussian) distributions
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
419 // - Increased overall speed by optimizing twist()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
420 // - Doubled speed of integer [0,n] generation
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
421 // - Fixed out-of-range number generation on 64-bit machines
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
422 // - Improved portability by substituting literal constants for long enum's
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
423 // - Changed license from GNU LGPL to BSD

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.