Scopira  20080306
linconrandom.h
1 
2 /*
3  * Copyright (c) 2002-2006 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED__SCOPIRA_TOOL_LINCONRANDOM_H__
15 #define __INCLUDED__SCOPIRA_TOOL_LINCONRANDOM_H__
16 
17 #include <assert.h>
18 
19 #include <scopira/tool/constmod.h>
20 #include <scopira/tool/platform.h>
21 
22 namespace scopira
23 {
24  namespace tool
25  {
26  template<class IntType, IntType a, IntType c, IntType m, IntType val>
27  class lincon_gen;
28 
35 
37  typedef minstd_rand0 lincon_core;
38 
39  class rand48;
40  }
41 }
42 
50 template<class IntType, IntType a, IntType c, IntType m, IntType val>
52 {
53  public:
54  typedef IntType result_type;
55 
56  static const bool has_fixed_range = true;
57  static const result_type min_value = ( c == 0 ? 1 : 0 );
58  static const result_type max_value = m-1;
59 
61  explicit lincon_gen(IntType x0 = 1)
62  : _x(x0) {
63  assert(c || x0); // if c == 0 and x(0) == 0 then x(n) = 0 for all n
64  // overflow check
65  // disabled because it gives spurious "divide by zero" gcc warnings
66  // assert(m == 0 || (a*(m-1)+c) % m == (c < a ? c-a+m : c-a));
67  }
68 
70  void seed(IntType x0) { assert(c || x0); _x = x0; }
71  result_type min(void) const { return c == 0 ? 1 : 0; }
72  result_type max(void) const { return m-1; }
73 
75  IntType operator()(void) {
76  _x = const_mod<IntType, m>::mult_add(a, _x, c);
77  return _x;
78  }
79 
80  bool validation(IntType x) const { return val == x; }
81  private:
82  IntType _x;
83 };
84 
94 {
95  public:
96  typedef int32_t result_type;
97  static const bool has_fixed_range = true;
98  static const int32_t min_value = 0;
99  //static const int32_t max_value = integer_traits<int32_t>::const_max;
100 
101  int32_t min(void) const { return 0; }
102  int32_t max(void) const { return std::numeric_limits<int32_t>::max(); }
103 
104  explicit rand48(int32_t x0 = 1) : lcf(cnv(x0)) { }
105  explicit rand48(uint64_t x0) : lcf(x0) { }
106  //template<class It> rand48(It& first, It last) : lcf(first, last) { }
107 
108  // compiler-generated copy ctor and assignment operator are fine
109 
110  void seed(int32_t x0 = 1) { lcf.seed(cnv(x0)); }
111  void seed(uint64_t x0) { lcf.seed(x0); }
112  //template<class It> void seed(It& first, It last) { lcf.seed(first,last); }
113 
114  int32_t operator()(void) { return static_cast<int32_t>(lcf() >> 17); }
115 
116  private:
117  scopira::tool::lincon_gen<uint64_t, uint64_t(0xDEECE66DUL) | (uint64_t(0x5) << 32),
118  0xB, uint64_t(1)<<48, 0> lcf;
119 
120  private:
121  static uint64_t cnv(int32_t x) { return (static_cast<uint64_t>(x) << 16) | 0x330e; }
122 };
123 
124 #endif
125 
Definition: linconrandom.h:93
lincon_gen< int, 48271, 0, 2147483647, 399268537 > minstd_rand
Definition: linconrandom.h:34
minstd_rand0 lincon_core
a typical random core
Definition: linconrandom.h:37
lincon_gen(IntType x0=1)
x0 is the seed
Definition: linconrandom.h:61
Definition: archiveflow.h:20
lincon_gen< int, 16807, 0, 2147483647, 1043618065 > minstd_rand0
Definition: linconrandom.h:27
void seed(IntType x0)
resets the seed
Definition: linconrandom.h:70
Definition: constmod.h:34
Definition: linconrandom.h:27
IntType operator()(void)
get next num
Definition: linconrandom.h:75