CORSIKA  @c8_version@
The framework to simulate particle cascades for astroparticle physics
MicroURNG.hpp
1 /*
2 Copyright 2010-2011, D. E. Shaw Research.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 
9 * Redistributions of source code must retain the above copyright
10  notice, this list of conditions, and the following disclaimer.
11 
12 * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions, and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16 * Neither the name of D. E. Shaw Research nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 #ifndef __MicroURNG_dot_hpp__
33 #define __MicroURNG_dot_hpp__
34 
35 #include <stdexcept>
36 #include <limits>
37 
38 namespace random_iterator_r123 {
78  template <typename CBRNG>
79  class MicroURNG {
80  // According to C++11, a URNG requires only a result_type,
81  // operator()(), min() and max() methods. Everything else
82  // (ctr_type, key_type, reset() method, etc.) is "value added"
83  // for the benefit of users that "know" that they're dealing with
84  // a MicroURNG.
85  public:
86  typedef CBRNG cbrng_type;
87  static const int BITS = 32;
88  typedef typename cbrng_type::ctr_type ctr_type;
89  typedef typename cbrng_type::key_type key_type;
90  typedef typename cbrng_type::ukey_type ukey_type;
91  typedef typename ctr_type::value_type result_type;
92 
93  RANDOM_ITERATOR_R123_STATIC_ASSERT(std::numeric_limits<result_type>::digits >= BITS,
94  "The result_type must have at least 32 bits");
95 
96  result_type operator()() {
97  if (last_elem == 0) {
98  // jam n into the high bits of c
99  const size_t W = std::numeric_limits<result_type>::digits;
100  ctr_type c = c0;
101  c[c0.size() - 1] |= n << (W - BITS);
102  rdata = b(c, k);
103  n++;
104  last_elem = rdata.size();
105  }
106  return rdata[--last_elem];
107  }
108  MicroURNG(cbrng_type _b, ctr_type _c0, ukey_type _uk)
109  : b(_b)
110  , c0(_c0)
111  , k(_uk)
112  , n(0)
113  , last_elem(0) {
114  chkhighbits();
115  }
116  MicroURNG(ctr_type _c0, ukey_type _uk)
117  : b()
118  , c0(_c0)
119  , k(_uk)
120  , n(0)
121  , last_elem(0) {
122  chkhighbits();
123  }
124 
125  // _Min and _Max work around a bug in the library shipped with MacOS Xcode 4.5.2.
126  // See the commment in conventional/Engine.hpp.
127  const static result_type _Min = 0;
128  const static result_type _Max = ~((result_type)0);
129 
130  static RANDOM_ITERATOR_R123_CONSTEXPR result_type min
131  RANDOM_ITERATOR_R123_NO_MACRO_SUBST() {
132  return _Min;
133  }
134  static RANDOM_ITERATOR_R123_CONSTEXPR result_type max
135  RANDOM_ITERATOR_R123_NO_MACRO_SUBST() {
136  return _Max;
137  }
138  // extra methods:
139  const ctr_type& counter() const { return c0; }
140  void reset(ctr_type _c0, ukey_type _uk) {
141  c0 = _c0;
142  chkhighbits();
143  k = _uk;
144  n = 0;
145  last_elem = 0;
146  }
147 
148  private:
149  cbrng_type b;
150  ctr_type c0;
151  key_type k;
152  RANDOM_ITERATOR_R123_ULONG_LONG n;
153  size_t last_elem;
154  ctr_type rdata;
155  void chkhighbits() {
156  result_type r = c0[c0.size() - 1];
157  result_type mask = ((uint64_t)std::numeric_limits<result_type>::max
158  RANDOM_ITERATOR_R123_NO_MACRO_SUBST()) >>
159  BITS;
160  if ((r & mask) != r)
161  throw std::runtime_error("MicroURNG: c0, does not have high bits clear");
162  }
163  };
164 } // namespace random_iterator_r123
165 #endif
Given a CBRNG whose ctr_type has an unsigned integral value_type, MicroURNG<CBRNG>(c, k) is a type that satisfies the requirements of a C++11 Uniform Random Number Generator.
Definition: MicroURNG.hpp:79