1 /* boost random/subtract_with_carry.hpp header file
3 * Copyright Jens Maurer 2002
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: subtract_with_carry.hpp 85813 2013-09-21 20:17:00Z jewillco $
16 #ifndef BOOST_RANDOM_SUBTRACT_WITH_CARRY_HPP
17 #define BOOST_RANDOM_SUBTRACT_WITH_CARRY_HPP
19 #include <boost/config/no_tr1/cmath.hpp> // std::pow
21 #include <algorithm> // std::equal
23 #include <boost/config.hpp>
24 #include <boost/limits.hpp>
25 #include <boost/cstdint.hpp>
26 #include <boost/static_assert.hpp>
27 #include <boost/integer/static_log2.hpp>
28 #include <boost/integer/integer_mask.hpp>
29 #include <boost/detail/workaround.hpp>
30 #include <boost/random/detail/config.hpp>
31 #include <boost/random/detail/seed.hpp>
32 #include <boost/random/detail/operators.hpp>
33 #include <boost/random/detail/seed_impl.hpp>
34 #include <boost/random/detail/generator_seed_seq.hpp>
35 #include <boost/random/linear_congruential.hpp>
43 struct subtract_with_carry_discard
45 template<class Engine>
46 static void apply(Engine& eng, boost::uintmax_t z)
48 typedef typename Engine::result_type IntType;
49 const std::size_t short_lag = Engine::short_lag;
50 const std::size_t long_lag = Engine::long_lag;
51 std::size_t k = eng.k;
52 IntType carry = eng.carry;
54 // increment k until it becomes 0.
56 std::size_t limit = (short_lag - k) < z?
57 short_lag : (k + static_cast<std::size_t>(z));
58 for(std::size_t j = k; j < limit; ++j) {
59 carry = eng.do_update(j, j + long_lag - short_lag, carry);
62 std::size_t limit = (long_lag - k) < z?
63 long_lag : (k + static_cast<std::size_t>(z));
64 std::size_t start = (k < short_lag ? short_lag : k);
65 for(std::size_t j = start; j < limit; ++j) {
66 carry = eng.do_update(j, j - short_lag, carry);
70 k = ((z % long_lag) + k) % long_lag;
73 // main loop: update full blocks from k = 0 to long_lag
74 for(std::size_t i = 0; i < (z - k) / long_lag; ++i) {
75 for(std::size_t j = 0; j < short_lag; ++j) {
76 carry = eng.do_update(j, j + long_lag - short_lag, carry);
78 for(std::size_t j = short_lag; j < long_lag; ++j) {
79 carry = eng.do_update(j, j - short_lag, carry);
83 // Update the last partial block
84 std::size_t limit = short_lag < k? short_lag : k;
85 for(std::size_t j = 0; j < limit; ++j) {
86 carry = eng.do_update(j, j + long_lag - short_lag, carry);
88 for(std::size_t j = short_lag; j < k; ++j) {
89 carry = eng.do_update(j, j - short_lag, carry);
100 * Instantiations of @c subtract_with_carry_engine model a
101 * \pseudo_random_number_generator. The algorithm is
105 * "A New Class of Random Number Generators", George
106 * Marsaglia and Arif Zaman, Annals of Applied Probability,
107 * Volume 1, Number 3 (1991), 462-480.
110 template<class IntType, std::size_t w, std::size_t s, std::size_t r>
111 class subtract_with_carry_engine
114 typedef IntType result_type;
115 BOOST_STATIC_CONSTANT(std::size_t, word_size = w);
116 BOOST_STATIC_CONSTANT(std::size_t, long_lag = r);
117 BOOST_STATIC_CONSTANT(std::size_t, short_lag = s);
118 BOOST_STATIC_CONSTANT(uint32_t, default_seed = 19780503u);
120 // Required by the old Boost.Random concepts
121 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
122 // Backwards compatibility
123 BOOST_STATIC_CONSTANT(result_type, modulus = (result_type(1) << w));
125 BOOST_STATIC_ASSERT(std::numeric_limits<result_type>::is_integer);
128 * Constructs a new @c subtract_with_carry_engine and seeds
129 * it with the default seed.
131 subtract_with_carry_engine() { seed(); }
133 * Constructs a new @c subtract_with_carry_engine and seeds
136 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(subtract_with_carry_engine,
140 * Constructs a new @c subtract_with_carry_engine and seeds
141 * it with values produced by @c seq.generate().
143 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(subtract_with_carry_engine,
147 * Constructs a new @c subtract_with_carry_engine and seeds
148 * it with values from a range. first is updated to point
149 * one past the last value consumed. If there are not
150 * enough elements in the range to fill the entire state of
151 * the generator, throws @c std::invalid_argument.
153 template<class It> subtract_with_carry_engine(It& first, It last)
154 { seed(first,last); }
156 // compiler-generated copy ctor and assignment operator are fine
158 /** Seeds the generator with the default seed. */
159 void seed() { seed(default_seed); }
160 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(subtract_with_carry_engine,
163 typedef linear_congruential_engine<uint32_t,40014,0,2147483563> gen_t;
164 gen_t intgen(static_cast<boost::uint32_t>(value == 0 ? default_seed : value));
165 detail::generator_seed_seq<gen_t> gen(intgen);
169 /** Seeds the generator with values produced by @c seq.generate(). */
170 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(subtract_with_carry, SeedSeq, seq)
172 detail::seed_array_int<w>(seq, x);
173 carry = (x[long_lag-1] == 0);
178 * Seeds the generator with values from a range. Updates @c first to
179 * point one past the last consumed value. If the range does not
180 * contain enough elements to fill the entire state of the generator,
181 * throws @c std::invalid_argument.
184 void seed(It& first, It last)
186 detail::fill_array_int<w>(first, last, x);
187 carry = (x[long_lag-1] == 0);
191 /** Returns the smallest value that the generator can produce. */
192 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
194 /** Returns the largest value that the generator can produce. */
195 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
196 { return boost::low_bits_mask_t<w>::sig_bits; }
198 /** Returns the next value of the generator. */
199 result_type operator()()
201 std::size_t short_index =
203 (k + long_lag - short_lag) :
205 carry = do_update(k, short_index, carry);
206 IntType result = x[k];
213 /** Advances the state of the generator by @c z. */
214 void discard(boost::uintmax_t z)
216 detail::subtract_with_carry_discard::apply(*this, z);
219 /** Fills a range with random values. */
221 void generate(It first, It last)
222 { detail::generate_from_int(*this, first, last); }
224 /** Writes a @c subtract_with_carry_engine to a @c std::ostream. */
225 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, subtract_with_carry_engine, f)
227 for(unsigned int j = 0; j < f.long_lag; ++j)
228 os << f.compute(j) << ' ';
233 /** Reads a @c subtract_with_carry_engine from a @c std::istream. */
234 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, subtract_with_carry_engine, f)
236 for(unsigned int j = 0; j < f.long_lag; ++j)
237 is >> f.x[j] >> std::ws;
244 * Returns true if the two generators will produce identical
245 * sequences of values.
247 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(subtract_with_carry_engine, x, y)
249 for(unsigned int j = 0; j < r; ++j)
250 if(x.compute(j) != y.compute(j))
256 * Returns true if the two generators will produce different
257 * sequences of values.
259 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(subtract_with_carry_engine)
262 /// \cond show_private
263 // returns x(i-r+index), where index is in 0..r-1
264 IntType compute(unsigned int index) const
266 return x[(k+index) % long_lag];
269 friend struct detail::subtract_with_carry_discard;
271 IntType do_update(std::size_t current, std::size_t short_index, IntType carry)
274 IntType temp = x[current] + carry;
275 if (x[short_index] >= temp) {
277 delta = x[short_index] - temp;
281 delta = modulus - temp + x[short_index];
289 // state representation; next output (state) is x(i)
290 // x[0] ... x[k] x[k+1] ... x[long_lag-1] represents
291 // x(i-k) ... x(i) x(i+1) ... x(i-k+long_lag-1)
292 // speed: base: 20-25 nsec
293 // ranlux_4: 230 nsec, ranlux_7: 430 nsec, ranlux_14: 810 nsec
294 // This state representation makes operator== and save/restore more
295 // difficult, because we've already computed "too much" and thus
296 // have to undo some steps to get at x(i-r) etc.
298 // state representation: next output (state) is x(i)
299 // x[0] ... x[k] x[k+1] ... x[long_lag-1] represents
300 // x(i-k) ... x(i) x(i-long_lag+1) ... x(i-k-1)
301 // speed: base 28 nsec
302 // ranlux_4: 370 nsec, ranlux_7: 688 nsec, ranlux_14: 1343 nsec
308 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
309 // A definition is required even for integral static constants
310 template<class IntType, std::size_t w, std::size_t s, std::size_t r>
311 const bool subtract_with_carry_engine<IntType, w, s, r>::has_fixed_range;
312 template<class IntType, std::size_t w, std::size_t s, std::size_t r>
313 const IntType subtract_with_carry_engine<IntType, w, s, r>::modulus;
314 template<class IntType, std::size_t w, std::size_t s, std::size_t r>
315 const std::size_t subtract_with_carry_engine<IntType, w, s, r>::word_size;
316 template<class IntType, std::size_t w, std::size_t s, std::size_t r>
317 const std::size_t subtract_with_carry_engine<IntType, w, s, r>::long_lag;
318 template<class IntType, std::size_t w, std::size_t s, std::size_t r>
319 const std::size_t subtract_with_carry_engine<IntType, w, s, r>::short_lag;
320 template<class IntType, std::size_t w, std::size_t s, std::size_t r>
321 const uint32_t subtract_with_carry_engine<IntType, w, s, r>::default_seed;
325 // use a floating-point representation to produce values in [0..1)
327 * Instantiations of \subtract_with_carry_01_engine model a
328 * \pseudo_random_number_generator. The algorithm is
332 * "A New Class of Random Number Generators", George
333 * Marsaglia and Arif Zaman, Annals of Applied Probability,
334 * Volume 1, Number 3 (1991), 462-480.
337 template<class RealType, std::size_t w, std::size_t s, std::size_t r>
338 class subtract_with_carry_01_engine
341 typedef RealType result_type;
342 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
343 BOOST_STATIC_CONSTANT(std::size_t, word_size = w);
344 BOOST_STATIC_CONSTANT(std::size_t, long_lag = r);
345 BOOST_STATIC_CONSTANT(std::size_t, short_lag = s);
346 BOOST_STATIC_CONSTANT(boost::uint32_t, default_seed = 19780503u);
348 BOOST_STATIC_ASSERT(!std::numeric_limits<result_type>::is_integer);
350 /** Creates a new \subtract_with_carry_01_engine using the default seed. */
351 subtract_with_carry_01_engine() { init_modulus(); seed(); }
352 /** Creates a new subtract_with_carry_01_engine and seeds it with value. */
353 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(subtract_with_carry_01_engine,
354 boost::uint32_t, value)
355 { init_modulus(); seed(value); }
357 * Creates a new \subtract_with_carry_01_engine and seeds with values
358 * produced by seq.generate().
360 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(subtract_with_carry_01_engine,
362 { init_modulus(); seed(seq); }
364 * Creates a new \subtract_with_carry_01_engine and seeds it with values
365 * from a range. Advances first to point one past the last consumed
366 * value. If the range does not contain enough elements to fill the
367 * entire state, throws @c std::invalid_argument.
369 template<class It> subtract_with_carry_01_engine(It& first, It last)
370 { init_modulus(); seed(first,last); }
373 /// \cond show_private
376 #ifndef BOOST_NO_STDC_NAMESPACE
377 // allow for Koenig lookup
380 _modulus = pow(RealType(2), RealType(word_size));
385 // compiler-generated copy ctor and assignment operator are fine
387 /** Seeds the generator with the default seed. */
388 void seed() { seed(default_seed); }
390 /** Seeds the generator with @c value. */
391 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(subtract_with_carry_01_engine,
392 boost::uint32_t, value)
394 typedef linear_congruential_engine<uint32_t, 40014, 0, 2147483563> gen_t;
395 gen_t intgen(value == 0 ? default_seed : value);
396 detail::generator_seed_seq<gen_t> gen(intgen);
400 /** Seeds the generator with values produced by @c seq.generate(). */
401 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(subtract_with_carry_01_engine,
404 detail::seed_array_real<w>(seq, x);
405 carry = (x[long_lag-1] ? 0 : 1 / _modulus);
410 * Seeds the generator with values from a range. Updates first to
411 * point one past the last consumed element. If there are not
412 * enough elements in the range to fill the entire state, throws
413 * @c std::invalid_argument.
416 void seed(It& first, It last)
418 detail::fill_array_real<w>(first, last, x);
419 carry = (x[long_lag-1] ? 0 : 1 / _modulus);
423 /** Returns the smallest value that the generator can produce. */
424 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
425 { return result_type(0); }
426 /** Returns the largest value that the generator can produce. */
427 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
428 { return result_type(1); }
430 /** Returns the next value of the generator. */
431 result_type operator()()
433 std::size_t short_index =
435 (k + long_lag - short_lag) :
437 carry = do_update(k, short_index, carry);
438 RealType result = x[k];
445 /** Advances the state of the generator by @c z. */
446 void discard(boost::uintmax_t z)
447 { detail::subtract_with_carry_discard::apply(*this, z); }
449 /** Fills a range with random values. */
451 void generate(Iter first, Iter last)
452 { detail::generate_from_real(*this, first, last); }
454 /** Writes a \subtract_with_carry_01_engine to a @c std::ostream. */
455 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, subtract_with_carry_01_engine, f)
457 std::ios_base::fmtflags oldflags =
458 os.flags(os.dec | os.fixed | os.left);
459 for(unsigned int j = 0; j < f.long_lag; ++j)
460 os << (f.compute(j) * f._modulus) << ' ';
461 os << (f.carry * f._modulus);
466 /** Reads a \subtract_with_carry_01_engine from a @c std::istream. */
467 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, subtract_with_carry_01_engine, f)
470 for(unsigned int j = 0; j < long_lag; ++j) {
471 is >> value >> std::ws;
472 f.x[j] = value / f._modulus;
475 f.carry = value / f._modulus;
480 /** Returns true if the two generators will produce identical sequences. */
481 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(subtract_with_carry_01_engine, x, y)
483 for(unsigned int j = 0; j < r; ++j)
484 if(x.compute(j) != y.compute(j))
489 /** Returns true if the two generators will produce different sequences. */
490 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(subtract_with_carry_01_engine)
493 /// \cond show_private
494 RealType compute(unsigned int index) const
496 return x[(k+index) % long_lag];
499 friend struct detail::subtract_with_carry_discard;
501 RealType do_update(std::size_t current, std::size_t short_index, RealType carry)
503 RealType delta = x[short_index] - x[current] - carry;
505 delta += RealType(1);
506 carry = RealType(1)/_modulus;
516 RealType x[long_lag];
520 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
521 // A definition is required even for integral static constants
522 template<class RealType, std::size_t w, std::size_t s, std::size_t r>
523 const bool subtract_with_carry_01_engine<RealType, w, s, r>::has_fixed_range;
524 template<class RealType, std::size_t w, std::size_t s, std::size_t r>
525 const std::size_t subtract_with_carry_01_engine<RealType, w, s, r>::word_size;
526 template<class RealType, std::size_t w, std::size_t s, std::size_t r>
527 const std::size_t subtract_with_carry_01_engine<RealType, w, s, r>::long_lag;
528 template<class RealType, std::size_t w, std::size_t s, std::size_t r>
529 const std::size_t subtract_with_carry_01_engine<RealType, w, s, r>::short_lag;
530 template<class RealType, std::size_t w, std::size_t s, std::size_t r>
531 const uint32_t subtract_with_carry_01_engine<RealType, w, s, r>::default_seed;
535 /// \cond show_deprecated
537 template<class IntType, IntType m, unsigned s, unsigned r, IntType v>
538 class subtract_with_carry :
539 public subtract_with_carry_engine<IntType,
540 boost::static_log2<m>::value, s, r>
542 typedef subtract_with_carry_engine<IntType,
543 boost::static_log2<m>::value, s, r> base_type;
545 subtract_with_carry() {}
546 BOOST_RANDOM_DETAIL_GENERATOR_CONSTRUCTOR(subtract_with_carry, Gen, gen)
548 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(subtract_with_carry,
552 subtract_with_carry(It& first, It last) : base_type(first, last) {}
553 void seed() { base_type::seed(); }
554 BOOST_RANDOM_DETAIL_GENERATOR_SEED(subtract_with_carry, Gen, gen)
556 detail::generator_seed_seq<Gen> seq(gen);
557 base_type::seed(seq);
559 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(subtract_with_carry, IntType, val)
560 { base_type::seed(val); }
562 void seed(It& first, It last) { base_type::seed(first, last); }
565 template<class RealType, int w, unsigned s, unsigned r, int v = 0>
566 class subtract_with_carry_01 :
567 public subtract_with_carry_01_engine<RealType, w, s, r>
569 typedef subtract_with_carry_01_engine<RealType, w, s, r> base_type;
571 subtract_with_carry_01() {}
572 BOOST_RANDOM_DETAIL_GENERATOR_CONSTRUCTOR(subtract_with_carry_01, Gen, gen)
574 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(subtract_with_carry_01,
578 subtract_with_carry_01(It& first, It last) : base_type(first, last) {}
579 void seed() { base_type::seed(); }
580 BOOST_RANDOM_DETAIL_GENERATOR_SEED(subtract_with_carry_01, Gen, gen)
582 detail::generator_seed_seq<Gen> seq(gen);
583 base_type::seed(seq);
585 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(subtract_with_carry_01, uint32_t, val)
586 { base_type::seed(val); }
588 void seed(It& first, It last) { base_type::seed(first, last); }
595 template<class Engine>
596 struct generator_bits;
598 template<class RealType, std::size_t w, std::size_t s, std::size_t r>
599 struct generator_bits<subtract_with_carry_01_engine<RealType, w, s, r> > {
600 static std::size_t value() { return w; }
603 template<class RealType, int w, unsigned s, unsigned r, int v>
604 struct generator_bits<subtract_with_carry_01<RealType, w, s, r, v> > {
605 static std::size_t value() { return w; }
610 } // namespace random
613 #endif // BOOST_RANDOM_SUBTRACT_WITH_CARRY_HPP