os/ossrv/ossrv_pub/boost_apis/boost/random/linear_congruential.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/linear_congruential.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: linear_congruential.hpp,v 1.22 2005/05/21 15:57:00 dgregor 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_LINEAR_CONGRUENTIAL_HPP
sl@0
    17
#define BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
sl@0
    18
sl@0
    19
#include <iostream>
sl@0
    20
#include <cassert>
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/random/detail/const_mod.hpp>
sl@0
    26
#include <boost/detail/workaround.hpp>
sl@0
    27
sl@0
    28
namespace boost {
sl@0
    29
namespace random {
sl@0
    30
sl@0
    31
// compile-time configurable linear congruential generator
sl@0
    32
template<class IntType, IntType a, IntType c, IntType m, IntType val>
sl@0
    33
class linear_congruential
sl@0
    34
{
sl@0
    35
public:
sl@0
    36
  typedef IntType result_type;
sl@0
    37
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
sl@0
    38
  static const bool has_fixed_range = true;
sl@0
    39
  static const result_type min_value = ( c == 0 ? 1 : 0 );
sl@0
    40
  static const result_type max_value = m-1;
sl@0
    41
#else
sl@0
    42
  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
sl@0
    43
#endif
sl@0
    44
  BOOST_STATIC_CONSTANT(IntType, multiplier = a);
sl@0
    45
  BOOST_STATIC_CONSTANT(IntType, increment = c);
sl@0
    46
  BOOST_STATIC_CONSTANT(IntType, modulus = m);
sl@0
    47
sl@0
    48
  // MSVC 6 and possibly others crash when encountering complicated integral
sl@0
    49
  // constant expressions.  Avoid the check for now.
sl@0
    50
  // BOOST_STATIC_ASSERT(m == 0 || a < m);
sl@0
    51
  // BOOST_STATIC_ASSERT(m == 0 || c < m);
sl@0
    52
sl@0
    53
  explicit linear_congruential(IntType x0 = 1)
sl@0
    54
    : _modulus(modulus), _x(_modulus ? (x0 % _modulus) : x0)
sl@0
    55
  { 
sl@0
    56
    assert(c || x0); /* if c == 0 and x(0) == 0 then x(n) = 0 for all n */
sl@0
    57
    // overflow check
sl@0
    58
    // disabled because it gives spurious "divide by zero" gcc warnings
sl@0
    59
    // assert(m == 0 || (a*(m-1)+c) % m == (c < a ? c-a+m : c-a)); 
sl@0
    60
sl@0
    61
    // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
sl@0
    62
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
sl@0
    63
    BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
sl@0
    64
#endif
sl@0
    65
  }
sl@0
    66
sl@0
    67
  template<class It>
sl@0
    68
  linear_congruential(It& first, It last) { seed(first, last); }
sl@0
    69
sl@0
    70
  // compiler-generated copy constructor and assignment operator are fine
sl@0
    71
  void seed(IntType x0 = 1)
sl@0
    72
  {
sl@0
    73
    assert(c || x0);
sl@0
    74
    _x = (_modulus ? (x0 % _modulus) : x0);
sl@0
    75
  }
sl@0
    76
sl@0
    77
  template<class It>
sl@0
    78
  void seed(It& first, It last)
sl@0
    79
  {
sl@0
    80
    if(first == last)
sl@0
    81
      throw std::invalid_argument("linear_congruential::seed");
sl@0
    82
    IntType value = *first++;
sl@0
    83
    _x = (_modulus ? (value % _modulus) : value);
sl@0
    84
  }
sl@0
    85
sl@0
    86
  result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return c == 0 ? 1 : 0; }
sl@0
    87
  result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return modulus-1; }
sl@0
    88
sl@0
    89
  IntType operator()()
sl@0
    90
  {
sl@0
    91
    _x = const_mod<IntType, m>::mult_add(a, _x, c);
sl@0
    92
    return _x;
sl@0
    93
  }
sl@0
    94
sl@0
    95
  static bool validation(IntType x) { return val == x; }
sl@0
    96
sl@0
    97
#ifdef BOOST_NO_OPERATORS_IN_NAMESPACE
sl@0
    98
    
sl@0
    99
  // Use a member function; Streamable concept not supported.
sl@0
   100
  bool operator==(const linear_congruential& rhs) const
sl@0
   101
  { return _x == rhs._x; }
sl@0
   102
  bool operator!=(const linear_congruential& rhs) const
sl@0
   103
  { return !(*this == rhs); }
sl@0
   104
sl@0
   105
#else 
sl@0
   106
  friend bool operator==(const linear_congruential& x,
sl@0
   107
                         const linear_congruential& y)
sl@0
   108
  { return x._x == y._x; }
sl@0
   109
  friend bool operator!=(const linear_congruential& x,
sl@0
   110
                         const linear_congruential& y)
sl@0
   111
  { return !(x == y); }
sl@0
   112
    
sl@0
   113
#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
sl@0
   114
  template<class CharT, class Traits>
sl@0
   115
  friend std::basic_ostream<CharT,Traits>&
sl@0
   116
  operator<<(std::basic_ostream<CharT,Traits>& os,
sl@0
   117
             const linear_congruential& lcg)
sl@0
   118
  {
sl@0
   119
    return os << lcg._x;
sl@0
   120
  }
sl@0
   121
sl@0
   122
  template<class CharT, class Traits>
sl@0
   123
  friend std::basic_istream<CharT,Traits>&
sl@0
   124
  operator>>(std::basic_istream<CharT,Traits>& is,
sl@0
   125
             linear_congruential& lcg)
sl@0
   126
  {
sl@0
   127
    return is >> lcg._x;
sl@0
   128
  }
sl@0
   129
 
sl@0
   130
private:
sl@0
   131
#endif
sl@0
   132
#endif
sl@0
   133
    
sl@0
   134
  IntType _modulus;   // work-around for gcc "divide by zero" warning in ctor
sl@0
   135
  IntType _x;
sl@0
   136
};
sl@0
   137
sl@0
   138
// probably needs the "no native streams" caveat for STLPort
sl@0
   139
#if !defined(__SGI_STL_PORT) && BOOST_WORKAROUND(__GNUC__, == 2)
sl@0
   140
template<class IntType, IntType a, IntType c, IntType m, IntType val>
sl@0
   141
std::ostream&
sl@0
   142
operator<<(std::ostream& os,
sl@0
   143
           const linear_congruential<IntType,a,c,m,val>& lcg)
sl@0
   144
{
sl@0
   145
    return os << lcg._x;
sl@0
   146
}
sl@0
   147
sl@0
   148
template<class IntType, IntType a, IntType c, IntType m, IntType val>
sl@0
   149
std::istream&
sl@0
   150
operator>>(std::istream& is,
sl@0
   151
           linear_congruential<IntType,a,c,m,val>& lcg)
sl@0
   152
{
sl@0
   153
    return is >> lcg._x;
sl@0
   154
}
sl@0
   155
#elif defined(BOOST_NO_OPERATORS_IN_NAMESPACE) || defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
sl@0
   156
template<class CharT, class Traits, class IntType, IntType a, IntType c, IntType m, IntType val>
sl@0
   157
std::basic_ostream<CharT,Traits>&
sl@0
   158
operator<<(std::basic_ostream<CharT,Traits>& os,
sl@0
   159
           const linear_congruential<IntType,a,c,m,val>& lcg)
sl@0
   160
{
sl@0
   161
    return os << lcg._x;
sl@0
   162
}
sl@0
   163
sl@0
   164
template<class CharT, class Traits, class IntType, IntType a, IntType c, IntType m, IntType val>
sl@0
   165
std::basic_istream<CharT,Traits>&
sl@0
   166
operator>>(std::basic_istream<CharT,Traits>& is,
sl@0
   167
           linear_congruential<IntType,a,c,m,val>& lcg)
sl@0
   168
{
sl@0
   169
    return is >> lcg._x;
sl@0
   170
}
sl@0
   171
#endif
sl@0
   172
sl@0
   173
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
sl@0
   174
//  A definition is required even for integral static constants
sl@0
   175
template<class IntType, IntType a, IntType c, IntType m, IntType val>
sl@0
   176
const bool linear_congruential<IntType, a, c, m, val>::has_fixed_range;
sl@0
   177
template<class IntType, IntType a, IntType c, IntType m, IntType val>
sl@0
   178
const typename linear_congruential<IntType, a, c, m, val>::result_type linear_congruential<IntType, a, c, m, val>::min_value;
sl@0
   179
template<class IntType, IntType a, IntType c, IntType m, IntType val>
sl@0
   180
const typename linear_congruential<IntType, a, c, m, val>::result_type linear_congruential<IntType, a, c, m, val>::max_value;
sl@0
   181
template<class IntType, IntType a, IntType c, IntType m, IntType val>
sl@0
   182
const IntType linear_congruential<IntType,a,c,m,val>::modulus;
sl@0
   183
#endif
sl@0
   184
sl@0
   185
} // namespace random
sl@0
   186
sl@0
   187
// validation values from the publications
sl@0
   188
typedef random::linear_congruential<int32_t, 16807, 0, 2147483647, 
sl@0
   189
  1043618065> minstd_rand0;
sl@0
   190
typedef random::linear_congruential<int32_t, 48271, 0, 2147483647,
sl@0
   191
  399268537> minstd_rand;
sl@0
   192
sl@0
   193
sl@0
   194
#if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T)
sl@0
   195
// emulate the lrand48() C library function; requires support for uint64_t
sl@0
   196
class rand48 
sl@0
   197
{
sl@0
   198
public:
sl@0
   199
  typedef int32_t result_type;
sl@0
   200
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
sl@0
   201
  static const bool has_fixed_range = true;
sl@0
   202
  static const int32_t min_value = 0;
sl@0
   203
  static const int32_t max_value = integer_traits<int32_t>::const_max;
sl@0
   204
#else
sl@0
   205
  enum { has_fixed_range = false };
sl@0
   206
#endif
sl@0
   207
  int32_t min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
sl@0
   208
  int32_t max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::numeric_limits<int32_t>::max BOOST_PREVENT_MACRO_SUBSTITUTION (); }
sl@0
   209
  
sl@0
   210
  explicit rand48(int32_t x0 = 1) : lcf(cnv(x0)) { }
sl@0
   211
  explicit rand48(uint64_t x0) : lcf(x0) { }
sl@0
   212
  template<class It> rand48(It& first, It last) : lcf(first, last) { }
sl@0
   213
  // compiler-generated copy ctor and assignment operator are fine
sl@0
   214
  void seed(int32_t x0 = 1) { lcf.seed(cnv(x0)); }
sl@0
   215
  void seed(uint64_t x0) { lcf.seed(x0); }
sl@0
   216
  template<class It> void seed(It& first, It last) { lcf.seed(first,last); }
sl@0
   217
sl@0
   218
  int32_t operator()() { return static_cast<int32_t>(lcf() >> 17); }
sl@0
   219
  // by experiment from lrand48()
sl@0
   220
  static bool validation(int32_t x) { return x == 1993516219; }
sl@0
   221
sl@0
   222
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
sl@0
   223
sl@0
   224
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
sl@0
   225
  template<class CharT,class Traits>
sl@0
   226
  friend std::basic_ostream<CharT,Traits>&
sl@0
   227
  operator<<(std::basic_ostream<CharT,Traits>& os, const rand48& r)
sl@0
   228
  { os << r.lcf; return os; }
sl@0
   229
sl@0
   230
  template<class CharT,class Traits>
sl@0
   231
  friend std::basic_istream<CharT,Traits>&
sl@0
   232
  operator>>(std::basic_istream<CharT,Traits>& is, rand48& r)
sl@0
   233
  { is >> r.lcf; return is; }
sl@0
   234
#endif
sl@0
   235
sl@0
   236
  friend bool operator==(const rand48& x, const rand48& y)
sl@0
   237
  { return x.lcf == y.lcf; }
sl@0
   238
  friend bool operator!=(const rand48& x, const rand48& y)
sl@0
   239
  { return !(x == y); }
sl@0
   240
#else
sl@0
   241
  // Use a member function; Streamable concept not supported.
sl@0
   242
  bool operator==(const rand48& rhs) const
sl@0
   243
  { return lcf == rhs.lcf; }
sl@0
   244
  bool operator!=(const rand48& rhs) const
sl@0
   245
  { return !(*this == rhs); }
sl@0
   246
#endif
sl@0
   247
private:
sl@0
   248
  random::linear_congruential<uint64_t,
sl@0
   249
    uint64_t(0xDEECE66DUL) | (uint64_t(0x5) << 32), // xxxxULL is not portable
sl@0
   250
    0xB, uint64_t(1)<<48, /* unknown */ 0> lcf;
sl@0
   251
  static uint64_t cnv(int32_t x) 
sl@0
   252
  { return (static_cast<uint64_t>(x) << 16) | 0x330e;  }
sl@0
   253
};
sl@0
   254
#endif /* !BOOST_NO_INT64_T && !BOOST_NO_INTEGRAL_INT64_T */
sl@0
   255
sl@0
   256
} // namespace boost
sl@0
   257
sl@0
   258
#endif // BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP