os/ossrv/ossrv_pub/boost_apis/boost/random/mersenne_twister.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* boost random/mersenne_twister.hpp header file
sl@0
     2
 *
sl@0
     3
 * Copyright Jens Maurer 2000-2001
sl@0
     4
 * Distributed under the Boost Software License, Version 1.0. (See
sl@0
     5
 * accompanying file LICENSE_1_0.txt or copy at
sl@0
     6
 * http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
 *
sl@0
     8
 * See http://www.boost.org for most recent version including documentation.
sl@0
     9
 *
sl@0
    10
 * $Id: mersenne_twister.hpp,v 1.20 2005/07/21 22:04:31 jmaurer Exp $
sl@0
    11
 *
sl@0
    12
 * Revision history
sl@0
    13
 *  2001-02-18  moved to individual header files
sl@0
    14
 */
sl@0
    15
sl@0
    16
#ifndef BOOST_RANDOM_MERSENNE_TWISTER_HPP
sl@0
    17
#define BOOST_RANDOM_MERSENNE_TWISTER_HPP
sl@0
    18
sl@0
    19
#include <iostream>
sl@0
    20
#include <algorithm>     // std::copy
sl@0
    21
#include <stdexcept>
sl@0
    22
#include <boost/config.hpp>
sl@0
    23
#include <boost/limits.hpp>
sl@0
    24
#include <boost/static_assert.hpp>
sl@0
    25
#include <boost/integer_traits.hpp>
sl@0
    26
#include <boost/cstdint.hpp>
sl@0
    27
#include <boost/random/linear_congruential.hpp>
sl@0
    28
#include <boost/detail/workaround.hpp>
sl@0
    29
#include <boost/random/detail/ptr_helper.hpp>
sl@0
    30
sl@0
    31
namespace boost {
sl@0
    32
namespace random {
sl@0
    33
sl@0
    34
// http://www.math.keio.ac.jp/matumoto/emt.html
sl@0
    35
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
    36
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
    37
class mersenne_twister
sl@0
    38
{
sl@0
    39
public:
sl@0
    40
  typedef UIntType result_type;
sl@0
    41
  BOOST_STATIC_CONSTANT(int, word_size = w);
sl@0
    42
  BOOST_STATIC_CONSTANT(int, state_size = n);
sl@0
    43
  BOOST_STATIC_CONSTANT(int, shift_size = m);
sl@0
    44
  BOOST_STATIC_CONSTANT(int, mask_bits = r);
sl@0
    45
  BOOST_STATIC_CONSTANT(UIntType, parameter_a = a);
sl@0
    46
  BOOST_STATIC_CONSTANT(int, output_u = u);
sl@0
    47
  BOOST_STATIC_CONSTANT(int, output_s = s);
sl@0
    48
  BOOST_STATIC_CONSTANT(UIntType, output_b = b);
sl@0
    49
  BOOST_STATIC_CONSTANT(int, output_t = t);
sl@0
    50
  BOOST_STATIC_CONSTANT(UIntType, output_c = c);
sl@0
    51
  BOOST_STATIC_CONSTANT(int, output_l = l);
sl@0
    52
sl@0
    53
  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
sl@0
    54
  
sl@0
    55
  mersenne_twister() { seed(); }
sl@0
    56
sl@0
    57
#if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x520)
sl@0
    58
  // Work around overload resolution problem (Gennadiy E. Rozental)
sl@0
    59
  explicit mersenne_twister(const UIntType& value)
sl@0
    60
#else
sl@0
    61
  explicit mersenne_twister(UIntType value)
sl@0
    62
#endif
sl@0
    63
  { seed(value); }
sl@0
    64
  template<class It> mersenne_twister(It& first, It last) { seed(first,last); }
sl@0
    65
sl@0
    66
  template<class Generator>
sl@0
    67
  explicit mersenne_twister(Generator & gen) { seed(gen); }
sl@0
    68
sl@0
    69
  // compiler-generated copy ctor and assignment operator are fine
sl@0
    70
sl@0
    71
  void seed() { seed(UIntType(5489)); }
sl@0
    72
sl@0
    73
#if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x520)
sl@0
    74
  // Work around overload resolution problem (Gennadiy E. Rozental)
sl@0
    75
  void seed(const UIntType& value)
sl@0
    76
#else
sl@0
    77
  void seed(UIntType value)
sl@0
    78
#endif
sl@0
    79
  {
sl@0
    80
    // New seeding algorithm from 
sl@0
    81
    // http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
sl@0
    82
    // In the previous versions, MSBs of the seed affected only MSBs of the
sl@0
    83
    // state x[].
sl@0
    84
    const UIntType mask = ~0u;
sl@0
    85
    x[0] = value & mask;
sl@0
    86
    for (i = 1; i < n; i++) {
sl@0
    87
      // See Knuth "The Art of Computer Programming" Vol. 2, 3rd ed., page 106
sl@0
    88
      x[i] = (1812433253UL * (x[i-1] ^ (x[i-1] >> (w-2))) + i) & mask;
sl@0
    89
    }
sl@0
    90
  }
sl@0
    91
sl@0
    92
  // For GCC, moving this function out-of-line prevents inlining, which may
sl@0
    93
  // reduce overall object code size.  However, MSVC does not grok
sl@0
    94
  // out-of-line definitions of member function templates.
sl@0
    95
  template<class Generator>
sl@0
    96
  void seed(Generator & gen)
sl@0
    97
  {
sl@0
    98
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
sl@0
    99
    BOOST_STATIC_ASSERT(!std::numeric_limits<result_type>::is_signed);
sl@0
   100
#endif
sl@0
   101
    // I could have used std::generate_n, but it takes "gen" by value
sl@0
   102
    for(int j = 0; j < n; j++)
sl@0
   103
      x[j] = gen();
sl@0
   104
    i = n;
sl@0
   105
  }
sl@0
   106
sl@0
   107
  template<class It>
sl@0
   108
  void seed(It& first, It last)
sl@0
   109
  {
sl@0
   110
    int j;
sl@0
   111
    for(j = 0; j < n && first != last; ++j, ++first)
sl@0
   112
      x[j] = *first;
sl@0
   113
    i = n;
sl@0
   114
    if(first == last && j < n)
sl@0
   115
      throw std::invalid_argument("mersenne_twister::seed");
sl@0
   116
  }
sl@0
   117
  
sl@0
   118
  result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
sl@0
   119
  result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const
sl@0
   120
  {
sl@0
   121
    // avoid "left shift count >= with of type" warning
sl@0
   122
    result_type res = 0;
sl@0
   123
    for(int i = 0; i < w; ++i)
sl@0
   124
      res |= (1u << i);
sl@0
   125
    return res;
sl@0
   126
  }
sl@0
   127
sl@0
   128
  result_type operator()();
sl@0
   129
  static bool validation(result_type v) { return val == v; }
sl@0
   130
sl@0
   131
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
sl@0
   132
sl@0
   133
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
sl@0
   134
  template<class CharT, class Traits>
sl@0
   135
  friend std::basic_ostream<CharT,Traits>&
sl@0
   136
  operator<<(std::basic_ostream<CharT,Traits>& os, const mersenne_twister& mt)
sl@0
   137
  {
sl@0
   138
    for(int j = 0; j < mt.state_size; ++j)
sl@0
   139
      os << mt.compute(j) << " ";
sl@0
   140
    return os;
sl@0
   141
  }
sl@0
   142
sl@0
   143
  template<class CharT, class Traits>
sl@0
   144
  friend std::basic_istream<CharT,Traits>&
sl@0
   145
  operator>>(std::basic_istream<CharT,Traits>& is, mersenne_twister& mt)
sl@0
   146
  {
sl@0
   147
    for(int j = 0; j < mt.state_size; ++j)
sl@0
   148
      is >> mt.x[j] >> std::ws;
sl@0
   149
    // MSVC (up to 7.1) and Borland (up to 5.64) don't handle the template
sl@0
   150
    // value parameter "n" available from the class template scope, so use
sl@0
   151
    // the static constant with the same value
sl@0
   152
    mt.i = mt.state_size;
sl@0
   153
    return is;
sl@0
   154
  }
sl@0
   155
#endif
sl@0
   156
sl@0
   157
  friend bool operator==(const mersenne_twister& x, const mersenne_twister& y)
sl@0
   158
  {
sl@0
   159
    for(int j = 0; j < state_size; ++j)
sl@0
   160
      if(x.compute(j) != y.compute(j))
sl@0
   161
        return false;
sl@0
   162
    return true;
sl@0
   163
  }
sl@0
   164
sl@0
   165
  friend bool operator!=(const mersenne_twister& x, const mersenne_twister& y)
sl@0
   166
  { return !(x == y); }
sl@0
   167
#else
sl@0
   168
  // Use a member function; Streamable concept not supported.
sl@0
   169
  bool operator==(const mersenne_twister& rhs) const
sl@0
   170
  {
sl@0
   171
    for(int j = 0; j < state_size; ++j)
sl@0
   172
      if(compute(j) != rhs.compute(j))
sl@0
   173
        return false;
sl@0
   174
    return true;
sl@0
   175
  }
sl@0
   176
sl@0
   177
  bool operator!=(const mersenne_twister& rhs) const
sl@0
   178
  { return !(*this == rhs); }
sl@0
   179
#endif
sl@0
   180
sl@0
   181
private:
sl@0
   182
  // returns x(i-n+index), where index is in 0..n-1
sl@0
   183
  UIntType compute(unsigned int index) const
sl@0
   184
  {
sl@0
   185
    // equivalent to (i-n+index) % 2n, but doesn't produce negative numbers
sl@0
   186
    return x[ (i + n + index) % (2*n) ];
sl@0
   187
  }
sl@0
   188
  void twist(int block);
sl@0
   189
sl@0
   190
  // state representation: next output is o(x(i))
sl@0
   191
  //   x[0]  ... x[k] x[k+1] ... x[n-1]     x[n]     ... x[2*n-1]   represents
sl@0
   192
  //  x(i-k) ... x(i) x(i+1) ... x(i-k+n-1) x(i-k-n) ... x[i(i-k-1)]
sl@0
   193
  // The goal is to always have x(i-n) ... x(i-1) available for
sl@0
   194
  // operator== and save/restore.
sl@0
   195
sl@0
   196
  UIntType x[2*n]; 
sl@0
   197
  int i;
sl@0
   198
};
sl@0
   199
sl@0
   200
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
sl@0
   201
//  A definition is required even for integral static constants
sl@0
   202
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   203
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   204
const bool mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::has_fixed_range;
sl@0
   205
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   206
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   207
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::state_size;
sl@0
   208
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   209
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   210
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::shift_size;
sl@0
   211
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   212
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   213
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::mask_bits;
sl@0
   214
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   215
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   216
const UIntType mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::parameter_a;
sl@0
   217
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   218
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   219
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::output_u;
sl@0
   220
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   221
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   222
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::output_s;
sl@0
   223
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   224
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   225
const UIntType mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::output_b;
sl@0
   226
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   227
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   228
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::output_t;
sl@0
   229
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   230
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   231
const UIntType mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::output_c;
sl@0
   232
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   233
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   234
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::output_l;
sl@0
   235
#endif
sl@0
   236
sl@0
   237
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   238
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   239
void mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::twist(int block)
sl@0
   240
{
sl@0
   241
  const UIntType upper_mask = (~0u) << r;
sl@0
   242
  const UIntType lower_mask = ~upper_mask;
sl@0
   243
sl@0
   244
  if(block == 0) {
sl@0
   245
    for(int j = n; j < 2*n; j++) {
sl@0
   246
      UIntType y = (x[j-n] & upper_mask) | (x[j-(n-1)] & lower_mask);
sl@0
   247
      x[j] = x[j-(n-m)] ^ (y >> 1) ^ (y&1 ? a : 0);
sl@0
   248
    }
sl@0
   249
  } else if (block == 1) {
sl@0
   250
    // split loop to avoid costly modulo operations
sl@0
   251
    {  // extra scope for MSVC brokenness w.r.t. for scope
sl@0
   252
      for(int j = 0; j < n-m; j++) {
sl@0
   253
        UIntType y = (x[j+n] & upper_mask) | (x[j+n+1] & lower_mask);
sl@0
   254
        x[j] = x[j+n+m] ^ (y >> 1) ^ (y&1 ? a : 0);
sl@0
   255
      }
sl@0
   256
    }
sl@0
   257
    
sl@0
   258
    for(int j = n-m; j < n-1; j++) {
sl@0
   259
      UIntType y = (x[j+n] & upper_mask) | (x[j+n+1] & lower_mask);
sl@0
   260
      x[j] = x[j-(n-m)] ^ (y >> 1) ^ (y&1 ? a : 0);
sl@0
   261
    }
sl@0
   262
    // last iteration
sl@0
   263
    UIntType y = (x[2*n-1] & upper_mask) | (x[0] & lower_mask);
sl@0
   264
    x[n-1] = x[m-1] ^ (y >> 1) ^ (y&1 ? a : 0);
sl@0
   265
    i = 0;
sl@0
   266
  }
sl@0
   267
}
sl@0
   268
sl@0
   269
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
sl@0
   270
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
sl@0
   271
inline typename mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::result_type
sl@0
   272
mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::operator()()
sl@0
   273
{
sl@0
   274
  if(i == n)
sl@0
   275
    twist(0);
sl@0
   276
  else if(i >= 2*n)
sl@0
   277
    twist(1);
sl@0
   278
  // Step 4
sl@0
   279
  UIntType z = x[i];
sl@0
   280
  ++i;
sl@0
   281
  z ^= (z >> u);
sl@0
   282
  z ^= ((z << s) & b);
sl@0
   283
  z ^= ((z << t) & c);
sl@0
   284
  z ^= (z >> l);
sl@0
   285
  return z;
sl@0
   286
}
sl@0
   287
sl@0
   288
} // namespace random
sl@0
   289
sl@0
   290
sl@0
   291
typedef random::mersenne_twister<uint32_t,32,351,175,19,0xccab8ee7,11,
sl@0
   292
  7,0x31b6ab00,15,0xffe50000,17, 0xa37d3c92> mt11213b;
sl@0
   293
sl@0
   294
// validation by experiment from mt19937.c
sl@0
   295
typedef random::mersenne_twister<uint32_t,32,624,397,31,0x9908b0df,11,
sl@0
   296
  7,0x9d2c5680,15,0xefc60000,18, 3346425566U> mt19937;
sl@0
   297
sl@0
   298
} // namespace boost
sl@0
   299
sl@0
   300
BOOST_RANDOM_PTR_HELPER_SPEC(boost::mt19937)
sl@0
   301
sl@0
   302
#endif // BOOST_RANDOM_MERSENNE_TWISTER_HPP