/usr/include/random/mt.h is in libblitz0-dev 1:0.10-3.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | // -*- C++ -*-
/*
* $Id$
*
* A C-program for MT19937: Integer version (1998/4/6)
* genrand() generates one pseudorandom unsigned integer (32bit)
* which is uniformly distributed among 0 to 2^32-1 for each
* call. sgenrand(seed) set initial values to the working area
* of 624 words. Before genrand(), sgenrand(seed) must be
* called once. (seed is any 32-bit integer except for 0).
* Coded by Takuji Nishimura, considering the suggestions by
* Topher Cooper and Marc Rieffel in July-Aug. 1997.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later
* version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Library General Public License for more details.
* You should have received a copy of the GNU Library General
* Public License along with this library; if not, write to the
* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura.
* When you use this, send an email to: matumoto@math.keio.ac.jp
* with an appropriate reference to your work.
*
* REFERENCE
* M. Matsumoto and T. Nishimura,
* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
* Pseudo-Random Number Generator",
* ACM Transactions on Modeling and Computer Simulation,
* Vol. 8, No. 1, January 1998, pp 3--30.
*
* See
* http://www.math.keio.ac.jp/~matumoto/emt.html
* and
* http://www.acm.org/pubs/citations/journals/tomacs/1998-8-1/p3-matsumoto/
*
*/
#ifndef BZ_RAND_MT
#define BZ_RAND_MT
#include <blitz/blitz.h>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <iterator>
#ifndef UINT_MAX
#include <limits.h>
#endif
#ifdef BZ_HAVE_BOOST_SERIALIZATION
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
#endif
BZ_NAMESPACE(ranlib)
#if UINT_MAX < 4294967295U
typedef unsigned long twist_int; // must be at least 32 bits
#else
typedef unsigned int twist_int;
#endif
class MersenneTwister
{
public:
private:
#if defined(BZ_HAVE_NAMESPACES) && defined(BZ_HAVE_STD)
typedef std::vector<twist_int> State;
#else
typedef vector<twist_int> State;
#endif
typedef State::size_type SizeType;
typedef State::iterator Iter;
// Implements Step 2 and half of Step 3 in the MN98 description
struct BitMixer {
BitMixer() : s0(0), K(0x9908b0df) {};
BitMixer(twist_int k) : s0(0), K(k) {};
// Return 0 if lsb of s1=0, a if lsb of s1=1.
inline twist_int low_mask (twist_int s1) const {
// This does not actually result in a branch because it's
// equivalent to ( -(s1 & 1u) ) & K
return (s1&1u) ? K : 0u;
}
// Return y>>1 in MN98 (step 2 + part of 3).
// y = (x[i] AND u) OR (x[i+1 mod n] AND ll), where s0=x[i] and
// s1=x[i+1 mod n]
inline twist_int high_mask (twist_int s1) const {
return ( (s0&0x80000000) | (s1&0x7fffffff) ) >>1;
}
// Calculate (half of) step 3 in MN98.
inline twist_int operator() (twist_int s1) {
// (y>>1) XOR (0 if lsb of s1=0, a if lsb of s1=1)
// x[i+m] is XORed in reload
// (Note that it is OK to call low_mask with s1 (x[i+1]) and not
// with y, like MN98 does, because s1&1 == y&1 by construction.
twist_int r = high_mask(s1) ^ low_mask(s1);
s0 = s1;
return r;
}
twist_int s0; // this is "x[i]" in the MN98 description
const twist_int K; // MN98 "a" vector
};
enum { N = 624,
PF = 397, // MN98 "m"
reference_seed = 4357 };
void initialize()
{
S.resize(N);
I = S.end();
}
public:
MersenneTwister() : b_(0x9D2C5680), c_(0xEFC60000)
{
initialize();
seed();
// There is a problem: static initialization + templates do not
// mix very well in C++. If you have a static member
// of a class template, there is no guarantee on its order iin
// static initialization. This MersenneTwister class is used
// elsewhere as a static member of a template class, and it is
// possible (in fact, I've done so) to create a static initializer
// that will invoke the seed() method of this object before its
// ctor has been called (result: crash).
// ANSI C++ is stranger than fiction.
// Currently the documentation forbids using RNGs from
// static initializers. There doesn't seem to be a good
// fix.
}
MersenneTwister(twist_int aa, twist_int bb, twist_int cc) :
twist_(aa), b_(bb), c_(cc)
{
initialize();
seed();
}
MersenneTwister(twist_int initial_seed) : b_(0x9D2C5680), c_(0xEFC60000)
{
initialize();
seed(initial_seed);
}
MersenneTwister(twist_int aa, twist_int bb, twist_int cc,
twist_int initial_seed) : twist_(aa), b_(bb), c_(cc)
{
initialize();
seed(initial_seed);
}
// Seed. Uses updated seed algorithm from mt19937ar. The old
// algorithm would yield sequences that were close from seeds that
// were close.
void seed (twist_int seed = reference_seed)
{
// seed cannot equal 0
if (seed == 0)
seed = reference_seed;
S[0] = seed & 0xFFFFFFFF;
for (SizeType mti=1; mti<S.size(); ++mti) {
S[mti] = (1812433253U * (S[mti-1] ^ (S[mti-1] >> 30)) + mti);
S[mti] &= 0xffffffffU;
}
reload();
}
// Seed by array, swiped directly from mt19937ar. Gives a larger
// initial seed space.
void seed (State seed_vector)
{
SizeType i, j, k;
seed(19650218U);
i=1; j=0;
const SizeType N=S.size();
const SizeType n=seed_vector.size();
k = (N>n ? N : n);
for (; k; k--) {
S[i] = (S[i] ^ ((S[i-1] ^ (S[i-1] >> 30)) * 1664525U))
+ seed_vector[j] + j; /* non linear */
S[i] &= 0xffffffffU; /* for WORDSIZE > 32 machines */
i++; j++;
if (i>=N) { S[0] = S[N-1]; i=1; }
if (j>=n) j=0;
}
for (k=N-1; k; k--) {
S[i] = (S[i] ^ ((S[i-1] ^ (S[i-1] >> 30)) * 1566083941UL))
- i; /* non linear */
S[i] &= 0xffffffffU; /* for WORDSIZE > 32 machines */
i++;
if (i>=N) { S[0] = S[N-1]; i=1; }
}
S[0] = 0x80000000U; /* MSB is 1; assuring non-zero initial array */
reload();
}
// generate a full new x array
void reload (void)
{
// This check is required because it is possible to call random()
// before the constructor. See the note above about static
// initialization.
Iter p0 = S.begin();
Iter pM = p0 + PF;
twist_ (S[0]); // set x[i]=x[0] in the twister (prime the pump)
for (Iter pf_end = S.begin()+(N-PF); p0 != pf_end; ++p0, ++pM)
// mt[kk] = mt[kk+m] XOR ((y>>1)XOR(mag01), as calc by BitMixer)
*p0 = *pM ^ twist_ (p0[1]);
// This is the "modulo part" where kk+m rolls over
pM = S.begin();
for (Iter s_end = S.begin()+(N-1); p0 != s_end; ++p0, ++pM)
*p0 = *pM ^ twist_ (p0[1]);
// and final element where kk+1 rolls over
*p0 = *pM ^ twist_ (S[0]);
I = S.begin();
}
inline twist_int random (void)
{
if (I >= S.end()) reload();
// get next word from array
twist_int y = *I++;
// Step 4+5 in MN98, multiply by tempering matrix
y ^= (y >> 11);
y ^= (y << 7) & b_;
y ^= (y << 15) & c_;
y ^= (y >> 18);
return y;
}
// functions for getting/setting state
class mt_state {
friend class MersenneTwister;
private:
State S;
State::difference_type I;
public:
mt_state() { }
mt_state(State s, State::difference_type i) : S(s), I(i) { }
mt_state(const std::string& s) {
std::istringstream is(s);
is >> I;
S = State(std::istream_iterator<twist_int>(is),
std::istream_iterator<twist_int>());
assert(!S.empty());
}
operator bool() const { return !S.empty(); }
std::string str() const {
if (S.empty())
return std::string();
std::ostringstream os;
os << I << " ";
std::copy(S.begin(), S.end(),
std::ostream_iterator<twist_int>(os," "));
return os.str();
}
#ifdef BZ_HAVE_BOOST_SERIALIZATION
friend class boost::serialization::access;
/** Serialization operator. Relies on the ability to serialize
std::vector. */
template<class T_arch>
void serialize(T_arch& ar, const unsigned int version) {
ar & S & I;
};
#endif
};
typedef mt_state T_state;
T_state getState() const { return T_state(S, I-S.begin()); }
std::string getStateString() const {
T_state tmp(S, I-S.begin());
return tmp.str();
}
void setState(const T_state& s) {
if (!s) {
std::cerr << "Error: state is empty" << std::endl;
return;
}
S = s.S;
I = S.begin() + s.I;
}
void setState(const std::string& s) {
T_state tmp(s);
setState(tmp);
}
private:
BitMixer twist_;
const twist_int b_,c_;
State S;
Iter I;
};
/** This class creates MersenneTwisters with different parameters
indexed by and ID number. */
class MersenneTwisterCreator
{
public:
static MersenneTwister create(unsigned int i) {
// We only have n different parameter sets
i = i % n;
return MersenneTwister(a_[i], b_[i], c_[i]);
};
private:
static const unsigned int n=48;
static const twist_int a_[n];
static const twist_int b_[n];
static const twist_int c_[n];
};
BZ_NAMESPACE_END
#endif // BZ_RAND_MT
|