2 * PCG Random Number Generation for C++
4 * Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * For additional information about the PCG random number generation scheme,
19 * including its license and other licensing options, visit
21 * http://www.pcg-random.org
25 * This code provides the reference implementation of the PCG family of
26 * random number generators. The code is complex because it implements
28 * - several members of the PCG family, specifically members corresponding
29 * to the output functions:
30 * - XSH RR (good for 64-bit state, 32-bit output)
31 * - XSH RS (good for 64-bit state, 32-bit output)
32 * - XSL RR (good for 128-bit state, 64-bit output)
33 * - RXS M XS (statistically most powerful generator)
34 * - XSL RR RR (good for 128-bit state, 128-bit output)
35 * - and RXS, RXS M, XSH, XSL (mostly for testing)
36 * - at potentially *arbitrary* bit sizes
37 * - with four different techniques for random streams (MCG, one-stream
38 * LCG, settable-stream LCG, unique-stream LCG)
39 * - and the extended generation schemes allowing arbitrary periods
40 * - with all features of C++11 random number generation (and more),
41 * some of which are somewhat painful, including
42 * - initializing with a SeedSequence which writes 32-bit values
43 * to memory, even though the state of the generator may not
44 * use 32-bit values (it might use smaller or larger integers)
45 * - I/O for RNGs and a prescribed format, which needs to handle
46 * the issue that 8-bit and 128-bit integers don't have working
47 * I/O routines (e.g., normally 8-bit = char, not integer)
48 * - equality and inequality for RNGs
49 * - and a number of convenience typedefs to mask all the complexity
51 * The code employes a fairly heavy level of abstraction, and has to deal
52 * with various C++ minutia. If you're looking to learn about how the PCG
53 * scheme works, you're probably best of starting with one of the other
54 * codebases (see www.pcg-random.org). But if you're curious about the
55 * constants for the various output functions used in those other, simpler,
56 * codebases, this code shows how they are calculated.
58 * On the positive side, at least there are convenience typedefs so that you
66 * uint32_t, // Output Type
67 * uint64_t, // State Type
68 * pcg_detail::xsh_rr_mixin<uint32_t, uint64_t>, true, // Output Func
69 * pcg_detail::specific_stream<uint64_t>, // Stream Kind
70 * pcg_detail::default_multiplier<uint64_t> // LCG Mult
75 #ifndef PCG_RAND_HPP_INCLUDED
76 #define PCG_RAND_HPP_INCLUDED 1
85 #include <type_traits>
92 * The pcg_extras namespace contains some support code that is likley to
93 * be useful for a variety of RNGs, including:
94 * - 128-bit int support for platforms where it isn't available natively
95 * - bit twiddling operations
96 * - I/O of 128-bit and 8-bit integers
97 * - Handling the evilness of SeedSeq
98 * - Support for efficiently producing random numbers less than a given
102 #include "pcg_extras.hpp"
104 namespace pcg_detail
{
106 using namespace pcg_extras
;
109 * The LCG generators need some constants to function. This code lets you
110 * look up the constant by *type*. For example
112 * default_multiplier<uint32_t>::multiplier()
114 * gives you the default multipler for 32-bit integers. We use the name
115 * of the constant and not a generic word like value to allow these classes
116 * to be used as mixins.
119 template <typename T
>
120 struct default_multiplier
{
121 // Not defined for an arbitrary type
124 template <typename T
>
125 struct default_increment
{
126 // Not defined for an arbitrary type
129 #define PCG_DEFINE_CONSTANT(type, what, kind, constant) \
131 struct what ## _ ## kind<type> { \
132 static constexpr type kind() { \
137 PCG_DEFINE_CONSTANT(uint8_t, default, multiplier
, 141U)
138 PCG_DEFINE_CONSTANT(uint8_t, default, increment
, 77U)
140 PCG_DEFINE_CONSTANT(uint16_t, default, multiplier
, 12829U)
141 PCG_DEFINE_CONSTANT(uint16_t, default, increment
, 47989U)
143 PCG_DEFINE_CONSTANT(uint32_t, default, multiplier
, 747796405U)
144 PCG_DEFINE_CONSTANT(uint32_t, default, increment
, 2891336453U)
146 PCG_DEFINE_CONSTANT(uint64_t, default, multiplier
, 6364136223846793005ULL)
147 PCG_DEFINE_CONSTANT(uint64_t, default, increment
, 1442695040888963407ULL)
149 PCG_DEFINE_CONSTANT(pcg128_t
, default, multiplier
,
150 PCG_128BIT_CONSTANT(2549297995355413924ULL,4865540595714422341ULL))
151 PCG_DEFINE_CONSTANT(pcg128_t
, default, increment
,
152 PCG_128BIT_CONSTANT(6364136223846793005ULL,1442695040888963407ULL))
156 * Each PCG generator is available in four variants, based on how it applies
157 * the additive constant for its underlying LCG; the variations are:
159 * single stream - all instances use the same fixed constant, thus
160 * the RNG always somewhere in same sequence
161 * mcg - adds zero, resulting in a single stream and reduced
163 * specific stream - the constant can be changed at any time, selecting
164 * a different random sequence
165 * unique stream - the constant is based on the memory addresss of the
166 * object, thus every RNG has its own unique sequence
168 * This variation is provided though mixin classes which define a function
169 * value called increment() that returns the nesessary additive constant.
179 template <typename itype
>
180 class unique_stream
{
182 static constexpr bool is_mcg
= false;
184 // Is never called, but is provided for symmetry with specific_stream
191 typedef itype state_type
;
193 constexpr itype
increment() const {
194 return itype(reinterpret_cast<unsigned long>(this) | 1);
197 constexpr itype
stream() const
199 return increment() >> 1;
202 static constexpr bool can_specify_stream
= false;
204 static constexpr size_t streams_pow2()
206 return (sizeof(itype
) < sizeof(size_t) ? sizeof(itype
)
207 : sizeof(size_t))*8 - 1u;
211 constexpr unique_stream() = default;
219 template <typename itype
>
222 static constexpr bool is_mcg
= true;
224 // Is never called, but is provided for symmetry with specific_stream
231 typedef itype state_type
;
233 static constexpr itype
increment() {
237 static constexpr bool can_specify_stream
= false;
239 static constexpr size_t streams_pow2()
245 constexpr no_stream() = default;
250 * single stream/sequence (oneseq)
253 template <typename itype
>
254 class oneseq_stream
: public default_increment
<itype
> {
256 static constexpr bool is_mcg
= false;
258 // Is never called, but is provided for symmetry with specific_stream
265 typedef itype state_type
;
267 static constexpr itype
stream()
269 return default_increment
<itype
>::increment() >> 1;
272 static constexpr bool can_specify_stream
= false;
274 static constexpr size_t streams_pow2()
280 constexpr oneseq_stream() = default;
288 template <typename itype
>
289 class specific_stream
{
291 static constexpr bool is_mcg
= false;
293 itype inc_
= default_increment
<itype
>::increment();
296 typedef itype state_type
;
297 typedef itype stream_state
;
299 constexpr itype
increment() const {
308 void set_stream(itype specific_seq
)
310 inc_
= (specific_seq
<< 1) | 1;
313 static constexpr bool can_specify_stream
= true;
315 static constexpr size_t streams_pow2()
317 return (sizeof(itype
)*8) - 1u;
321 specific_stream() = default;
323 specific_stream(itype specific_seq
)
324 : inc_((specific_seq
<< 1) | itype(1U))
326 // Nothing (else) to do.
332 * This is where it all comes together. This function joins together three
333 * mixin classes which define
334 * - the LCG additive constant (the stream)
335 * - the LCG multiplier
336 * - the output function
337 * in addition, we specify the type of the LCG state, and the result type,
338 * and whether to use the pre-advance version of the state for the output
339 * (increasing instruction-level parallelism) or the post-advance version
340 * (reducing register pressure).
342 * Given the high level of parameterization, the code has to use some
343 * template-metaprogramming tricks to handle some of the suble variations
347 template <typename xtype
, typename itype
,
348 typename output_mixin
,
349 bool output_previous
= true,
350 typename stream_mixin
= oneseq_stream
<itype
>,
351 typename multiplier_mixin
= default_multiplier
<itype
> >
352 class engine
: protected output_mixin
,
354 protected multiplier_mixin
{
358 struct can_specify_stream_tag
{};
359 struct no_specifiable_stream_tag
{};
361 using stream_mixin::increment
;
362 using multiplier_mixin::multiplier
;
365 typedef xtype result_type
;
366 typedef itype state_type
;
368 static constexpr size_t period_pow2()
370 return sizeof(state_type
)*8 - 2*stream_mixin::is_mcg
;
373 // It would be nice to use std::numeric_limits for these, but
374 // we can't be sure that it'd be defined for the 128-bit types.
376 static constexpr result_type
min()
378 return result_type(0UL);
381 static constexpr result_type
max()
383 return ~result_type(0UL);
387 itype
bump(itype state
)
389 return state
* multiplier() + increment();
392 itype
base_generate()
394 return state_
= bump(state_
);
397 itype
base_generate0()
399 itype old_state
= state_
;
400 state_
= bump(state_
);
405 result_type
operator()()
408 return this->output(base_generate0());
410 return this->output(base_generate());
413 result_type
operator()(result_type upper_bound
)
415 return bounded_rand(*this, upper_bound
);
419 static itype
advance(itype state
, itype delta
,
420 itype cur_mult
, itype cur_plus
);
422 static itype
distance(itype cur_state
, itype newstate
, itype cur_mult
,
423 itype cur_plus
, itype mask
= ~itype(0U));
425 itype
distance(itype newstate
, itype mask
= ~itype(0U)) const
427 return distance(state_
, newstate
, multiplier(), increment(), mask
);
431 void advance(itype delta
)
433 state_
= advance(state_
, delta
, this->multiplier(), this->increment());
436 void backstep(itype delta
)
441 void discard(itype delta
)
448 if (stream_mixin::is_mcg
) {
449 // For MCGs, the low order two bits never change. In this
450 // implementation, we keep them fixed at 3 to make this test
458 engine(itype state
= itype(0xcafef00dd15ea5e5ULL
))
459 : state_(this->is_mcg
? state
|state_type(3U)
460 : bump(state
+ this->increment()))
462 // Nothing else to do.
465 // This function may or may not exist. It thus has to be a template
466 // to use SFINAE; users don't have to worry about its template-ness.
468 template <typename sm
= stream_mixin
>
469 engine(itype state
, typename
sm::stream_state stream_seed
)
470 : stream_mixin(stream_seed
),
471 state_(this->is_mcg
? state
|state_type(3U)
472 : bump(state
+ this->increment()))
474 // Nothing else to do.
477 template<typename SeedSeq
>
478 engine(SeedSeq
&& seedSeq
, typename
std::enable_if
<
479 !stream_mixin::can_specify_stream
480 && !std::is_convertible
<SeedSeq
, itype
>::value
481 && !std::is_convertible
<SeedSeq
, engine
>::value
,
482 no_specifiable_stream_tag
>::type
= {})
483 : engine(generate_one
<itype
>(std::forward
<SeedSeq
>(seedSeq
)))
485 // Nothing else to do.
488 template<typename SeedSeq
>
489 engine(SeedSeq
&& seedSeq
, typename
std::enable_if
<
490 stream_mixin::can_specify_stream
491 && !std::is_convertible
<SeedSeq
, itype
>::value
492 && !std::is_convertible
<SeedSeq
, engine
>::value
,
493 can_specify_stream_tag
>::type
= {})
494 : engine(generate_one
<itype
,1,2>(seedSeq
),
495 generate_one
<itype
,0,2>(seedSeq
))
497 // Nothing else to do.
501 template<typename
... Args
>
502 void seed(Args
&&... args
)
504 new (this) engine(std::forward
<Args
>(args
)...);
507 template <typename xtype1
, typename itype1
,
508 typename output_mixin1
, bool output_previous1
,
509 typename stream_mixin_lhs
, typename multiplier_mixin_lhs
,
510 typename stream_mixin_rhs
, typename multiplier_mixin_rhs
>
511 friend bool operator==(const engine
<xtype1
,itype1
,
512 output_mixin1
,output_previous1
,
513 stream_mixin_lhs
, multiplier_mixin_lhs
>&,
514 const engine
<xtype1
,itype1
,
515 output_mixin1
,output_previous1
,
516 stream_mixin_rhs
, multiplier_mixin_rhs
>&);
518 template <typename xtype1
, typename itype1
,
519 typename output_mixin1
, bool output_previous1
,
520 typename stream_mixin_lhs
, typename multiplier_mixin_lhs
,
521 typename stream_mixin_rhs
, typename multiplier_mixin_rhs
>
522 friend itype1
operator-(const engine
<xtype1
,itype1
,
523 output_mixin1
,output_previous1
,
524 stream_mixin_lhs
, multiplier_mixin_lhs
>&,
525 const engine
<xtype1
,itype1
,
526 output_mixin1
,output_previous1
,
527 stream_mixin_rhs
, multiplier_mixin_rhs
>&);
529 template <typename CharT
, typename Traits
,
530 typename xtype1
, typename itype1
,
531 typename output_mixin1
, bool output_previous1
,
532 typename stream_mixin1
, typename multiplier_mixin1
>
533 friend std::basic_ostream
<CharT
,Traits
>&
534 operator<<(std::basic_ostream
<CharT
,Traits
>& out
,
535 const engine
<xtype1
,itype1
,
536 output_mixin1
,output_previous1
,
537 stream_mixin1
, multiplier_mixin1
>&);
539 template <typename CharT
, typename Traits
,
540 typename xtype1
, typename itype1
,
541 typename output_mixin1
, bool output_previous1
,
542 typename stream_mixin1
, typename multiplier_mixin1
>
543 friend std::basic_istream
<CharT
,Traits
>&
544 operator>>(std::basic_istream
<CharT
,Traits
>& in
,
545 engine
<xtype1
, itype1
,
546 output_mixin1
, output_previous1
,
547 stream_mixin1
, multiplier_mixin1
>& rng
);
550 template <typename CharT
, typename Traits
,
551 typename xtype
, typename itype
,
552 typename output_mixin
, bool output_previous
,
553 typename stream_mixin
, typename multiplier_mixin
>
554 std::basic_ostream
<CharT
,Traits
>&
555 operator<<(std::basic_ostream
<CharT
,Traits
>& out
,
556 const engine
<xtype
,itype
,
557 output_mixin
,output_previous
,
558 stream_mixin
, multiplier_mixin
>& rng
)
560 auto orig_flags
= out
.flags(std::ios_base::dec
| std::ios_base::left
);
561 auto space
= out
.widen(' ');
562 auto orig_fill
= out
.fill();
564 out
<< rng
.multiplier() << space
565 << rng
.increment() << space
568 out
.flags(orig_flags
);
574 template <typename CharT
, typename Traits
,
575 typename xtype
, typename itype
,
576 typename output_mixin
, bool output_previous
,
577 typename stream_mixin
, typename multiplier_mixin
>
578 std::basic_istream
<CharT
,Traits
>&
579 operator>>(std::basic_istream
<CharT
,Traits
>& in
,
581 output_mixin
,output_previous
,
582 stream_mixin
, multiplier_mixin
>& rng
)
584 auto orig_flags
= in
.flags(std::ios_base::dec
| std::ios_base::skipws
);
586 itype multiplier
, increment
, state
;
587 in
>> multiplier
>> increment
>> state
;
591 if (multiplier
!= rng
.multiplier()) {
593 } else if (rng
.can_specify_stream
) {
594 rng
.set_stream(increment
>> 1);
595 } else if (increment
!= rng
.increment()) {
601 in
.clear(std::ios::failbit
);
605 in
.flags(orig_flags
);
610 template <typename xtype
, typename itype
,
611 typename output_mixin
, bool output_previous
,
612 typename stream_mixin
, typename multiplier_mixin
>
613 itype engine
<xtype
,itype
,output_mixin
,output_previous
,stream_mixin
,
614 multiplier_mixin
>::advance(
615 itype state
, itype delta
, itype cur_mult
, itype cur_plus
)
617 // The method used here is based on Brown, "Random Number Generation
618 // with Arbitrary Stride,", Transactions of the American Nuclear
619 // Society (Nov. 1994). The algorithm is very similar to fast
622 // Even though delta is an unsigned integer, we can pass a
623 // signed integer to go backwards, it just goes "the long way round".
625 constexpr itype ZERO
= 0u; // itype may be a non-trivial types, so
626 constexpr itype ONE
= 1u; // we define some ugly constants.
629 while (delta
> ZERO
) {
631 acc_mult
*= cur_mult
;
632 acc_plus
= acc_plus
*cur_mult
+ cur_plus
;
634 cur_plus
= (cur_mult
+ONE
)*cur_plus
;
635 cur_mult
*= cur_mult
;
638 return acc_mult
* state
+ acc_plus
;
641 template <typename xtype
, typename itype
,
642 typename output_mixin
, bool output_previous
,
643 typename stream_mixin
, typename multiplier_mixin
>
644 itype engine
<xtype
,itype
,output_mixin
,output_previous
,stream_mixin
,
645 multiplier_mixin
>::distance(
646 itype cur_state
, itype newstate
, itype cur_mult
, itype cur_plus
, itype mask
)
648 constexpr itype ONE
= 1u; // itype could be weird, so use constant
649 itype the_bit
= stream_mixin::is_mcg
? itype(4u) : itype(1u);
651 while ((cur_state
& mask
) != (newstate
& mask
)) {
652 if ((cur_state
& the_bit
) != (newstate
& the_bit
)) {
653 cur_state
= cur_state
* cur_mult
+ cur_plus
;
656 assert((cur_state
& the_bit
) == (newstate
& the_bit
));
658 cur_plus
= (cur_mult
+ONE
)*cur_plus
;
659 cur_mult
*= cur_mult
;
661 return stream_mixin::is_mcg
? distance
>> 2 : distance
;
664 template <typename xtype
, typename itype
,
665 typename output_mixin
, bool output_previous
,
666 typename stream_mixin_lhs
, typename multiplier_mixin_lhs
,
667 typename stream_mixin_rhs
, typename multiplier_mixin_rhs
>
668 itype
operator-(const engine
<xtype
,itype
,
669 output_mixin
,output_previous
,
670 stream_mixin_lhs
, multiplier_mixin_lhs
>& lhs
,
671 const engine
<xtype
,itype
,
672 output_mixin
,output_previous
,
673 stream_mixin_rhs
, multiplier_mixin_rhs
>& rhs
)
675 if (lhs
.multiplier() != rhs
.multiplier()
676 || lhs
.increment() != rhs
.increment())
677 throw std::logic_error("incomparable generators");
678 return rhs
.distance(lhs
.state_
);
682 template <typename xtype
, typename itype
,
683 typename output_mixin
, bool output_previous
,
684 typename stream_mixin_lhs
, typename multiplier_mixin_lhs
,
685 typename stream_mixin_rhs
, typename multiplier_mixin_rhs
>
686 bool operator==(const engine
<xtype
,itype
,
687 output_mixin
,output_previous
,
688 stream_mixin_lhs
, multiplier_mixin_lhs
>& lhs
,
689 const engine
<xtype
,itype
,
690 output_mixin
,output_previous
,
691 stream_mixin_rhs
, multiplier_mixin_rhs
>& rhs
)
693 return (lhs
.multiplier() == rhs
.multiplier())
694 && (lhs
.increment() == rhs
.increment())
695 && (lhs
.state_
== rhs
.state_
);
698 template <typename xtype
, typename itype
,
699 typename output_mixin
, bool output_previous
,
700 typename stream_mixin_lhs
, typename multiplier_mixin_lhs
,
701 typename stream_mixin_rhs
, typename multiplier_mixin_rhs
>
702 inline bool operator!=(const engine
<xtype
,itype
,
703 output_mixin
,output_previous
,
704 stream_mixin_lhs
, multiplier_mixin_lhs
>& lhs
,
705 const engine
<xtype
,itype
,
706 output_mixin
,output_previous
,
707 stream_mixin_rhs
, multiplier_mixin_rhs
>& rhs
)
709 return !operator==(lhs
,rhs
);
713 template <typename xtype
, typename itype
,
714 template<typename XT
,typename IT
> class output_mixin
,
715 bool output_previous
= (sizeof(itype
) <= 8)>
716 using oneseq_base
= engine
<xtype
, itype
,
717 output_mixin
<xtype
, itype
>, output_previous
,
718 oneseq_stream
<itype
> >;
720 template <typename xtype
, typename itype
,
721 template<typename XT
,typename IT
> class output_mixin
,
722 bool output_previous
= (sizeof(itype
) <= 8)>
723 using unique_base
= engine
<xtype
, itype
,
724 output_mixin
<xtype
, itype
>, output_previous
,
725 unique_stream
<itype
> >;
727 template <typename xtype
, typename itype
,
728 template<typename XT
,typename IT
> class output_mixin
,
729 bool output_previous
= (sizeof(itype
) <= 8)>
730 using setseq_base
= engine
<xtype
, itype
,
731 output_mixin
<xtype
, itype
>, output_previous
,
732 specific_stream
<itype
> >;
734 template <typename xtype
, typename itype
,
735 template<typename XT
,typename IT
> class output_mixin
,
736 bool output_previous
= (sizeof(itype
) <= 8)>
737 using mcg_base
= engine
<xtype
, itype
,
738 output_mixin
<xtype
, itype
>, output_previous
,
744 * These are the core of the PCG generation scheme. They specify how to
745 * turn the base LCG's internal state into the output value of the final
748 * They're implemented as mixin classes.
750 * All of the classes have code that is written to allow it to be applied
751 * at *arbitrary* bit sizes, although in practice they'll only be used at
752 * standard sizes supported by C++.
756 * XSH RS -- high xorshift, followed by a random shift
758 * Fast. A good performer.
761 template <typename xtype
, typename itype
>
762 struct xsh_rs_mixin
{
763 static xtype
output(itype internal
)
765 constexpr bitcount_t bits
= bitcount_t(sizeof(itype
) * 8);
766 constexpr bitcount_t xtypebits
= bitcount_t(sizeof(xtype
) * 8);
767 constexpr bitcount_t sparebits
= bits
- xtypebits
;
768 constexpr bitcount_t opbits
=
769 sparebits
-5 >= 64 ? 5
770 : sparebits
-4 >= 32 ? 4
771 : sparebits
-3 >= 16 ? 3
772 : sparebits
-2 >= 4 ? 2
773 : sparebits
-1 >= 1 ? 1
775 constexpr bitcount_t mask
= (1 << opbits
) - 1;
776 constexpr bitcount_t maxrandshift
= mask
;
777 constexpr bitcount_t topspare
= opbits
;
778 constexpr bitcount_t bottomspare
= sparebits
- topspare
;
779 constexpr bitcount_t xshift
= topspare
+ (xtypebits
+maxrandshift
)/2;
781 opbits
? bitcount_t(internal
>> (bits
- opbits
)) & mask
: 0;
782 internal
^= internal
>> xshift
;
783 xtype result
= xtype(internal
>> (bottomspare
- maxrandshift
+ rshift
));
789 * XSH RR -- high xorshift, followed by a random rotate
791 * Fast. A good performer. Slightly better statistically than XSH RS.
794 template <typename xtype
, typename itype
>
795 struct xsh_rr_mixin
{
796 static xtype
output(itype internal
)
798 constexpr bitcount_t bits
= bitcount_t(sizeof(itype
) * 8);
799 constexpr bitcount_t xtypebits
= bitcount_t(sizeof(xtype
)*8);
800 constexpr bitcount_t sparebits
= bits
- xtypebits
;
801 constexpr bitcount_t wantedopbits
=
803 : xtypebits
>= 64 ? 6
804 : xtypebits
>= 32 ? 5
805 : xtypebits
>= 16 ? 4
807 constexpr bitcount_t opbits
=
808 sparebits
>= wantedopbits
? wantedopbits
810 constexpr bitcount_t amplifier
= wantedopbits
- opbits
;
811 constexpr bitcount_t mask
= (1 << opbits
) - 1;
812 constexpr bitcount_t topspare
= opbits
;
813 constexpr bitcount_t bottomspare
= sparebits
- topspare
;
814 constexpr bitcount_t xshift
= (topspare
+ xtypebits
)/2;
815 bitcount_t rot
= opbits
? bitcount_t(internal
>> (bits
- opbits
)) & mask
817 bitcount_t amprot
= (rot
<< amplifier
) & mask
;
818 internal
^= internal
>> xshift
;
819 xtype result
= xtype(internal
>> bottomspare
);
820 result
= rotr(result
, amprot
);
826 * RXS -- random xorshift
829 template <typename xtype
, typename itype
>
831 static xtype
output_rxs(itype internal
)
833 constexpr bitcount_t bits
= bitcount_t(sizeof(itype
) * 8);
834 constexpr bitcount_t xtypebits
= bitcount_t(sizeof(xtype
)*8);
835 constexpr bitcount_t shift
= bits
- xtypebits
;
836 constexpr bitcount_t extrashift
= (xtypebits
- shift
)/2;
837 bitcount_t rshift
= shift
> 64+8 ? (internal
>> (bits
- 6)) & 63
838 : shift
> 32+4 ? (internal
>> (bits
- 5)) & 31
839 : shift
> 16+2 ? (internal
>> (bits
- 4)) & 15
840 : shift
> 8+1 ? (internal
>> (bits
- 3)) & 7
841 : shift
> 4+1 ? (internal
>> (bits
- 2)) & 3
842 : shift
> 2+1 ? (internal
>> (bits
- 1)) & 1
844 internal
^= internal
>> (shift
+ extrashift
- rshift
);
845 xtype result
= internal
>> rshift
;
851 * RXS M XS -- random xorshift, mcg multiply, fixed xorshift
853 * The most statistically powerful generator, but all those steps
854 * make it slower than some of the others. We give it the rottenest jobs.
856 * Because it's usually used in contexts where the state type and the
857 * result type are the same, it is a permutation and is thus invertable.
858 * We thus provide a function to invert it. This function is used to
859 * for the "inside out" generator used by the extended generator.
862 /* Defined type-based concepts for the multiplication step. They're actually
863 * all derived by truncating the 128-bit, which was computed to be a good
864 * "universal" constant.
867 template <typename T
>
868 struct mcg_multiplier
{
869 // Not defined for an arbitrary type
872 template <typename T
>
873 struct mcg_unmultiplier
{
874 // Not defined for an arbitrary type
877 PCG_DEFINE_CONSTANT(uint8_t, mcg
, multiplier
, 217U)
878 PCG_DEFINE_CONSTANT(uint8_t, mcg
, unmultiplier
, 105U)
880 PCG_DEFINE_CONSTANT(uint16_t, mcg
, multiplier
, 62169U)
881 PCG_DEFINE_CONSTANT(uint16_t, mcg
, unmultiplier
, 28009U)
883 PCG_DEFINE_CONSTANT(uint32_t, mcg
, multiplier
, 277803737U)
884 PCG_DEFINE_CONSTANT(uint32_t, mcg
, unmultiplier
, 2897767785U)
886 PCG_DEFINE_CONSTANT(uint64_t, mcg
, multiplier
, 12605985483714917081ULL)
887 PCG_DEFINE_CONSTANT(uint64_t, mcg
, unmultiplier
, 15009553638781119849ULL)
889 PCG_DEFINE_CONSTANT(pcg128_t
, mcg
, multiplier
,
890 PCG_128BIT_CONSTANT(17766728186571221404ULL, 12605985483714917081ULL))
891 PCG_DEFINE_CONSTANT(pcg128_t
, mcg
, unmultiplier
,
892 PCG_128BIT_CONSTANT(14422606686972528997ULL, 15009553638781119849ULL))
895 template <typename xtype
, typename itype
>
896 struct rxs_m_xs_mixin
{
897 static xtype
output(itype internal
)
899 constexpr bitcount_t xtypebits
= bitcount_t(sizeof(xtype
) * 8);
900 constexpr bitcount_t bits
= bitcount_t(sizeof(itype
) * 8);
901 constexpr bitcount_t opbits
= xtypebits
>= 128 ? 6
902 : xtypebits
>= 64 ? 5
903 : xtypebits
>= 32 ? 4
904 : xtypebits
>= 16 ? 3
906 constexpr bitcount_t shift
= bits
- xtypebits
;
907 constexpr bitcount_t mask
= (1 << opbits
) - 1;
909 opbits
? bitcount_t(internal
>> (bits
- opbits
)) & mask
: 0;
910 internal
^= internal
>> (opbits
+ rshift
);
911 internal
*= mcg_multiplier
<itype
>::multiplier();
912 xtype result
= internal
>> shift
;
913 result
^= result
>> ((2U*xtypebits
+2U)/3U);
917 static itype
unoutput(itype internal
)
919 constexpr bitcount_t bits
= bitcount_t(sizeof(itype
) * 8);
920 constexpr bitcount_t opbits
= bits
>= 128 ? 6
925 constexpr bitcount_t mask
= (1 << opbits
) - 1;
927 internal
= unxorshift(internal
, bits
, (2U*bits
+2U)/3U);
929 internal
*= mcg_unmultiplier
<itype
>::unmultiplier();
931 bitcount_t rshift
= opbits
? (internal
>> (bits
- opbits
)) & mask
: 0;
932 internal
= unxorshift(internal
, bits
, opbits
+ rshift
);
940 * RXS M -- random xorshift, mcg multiply
943 template <typename xtype
, typename itype
>
945 static xtype
output(itype internal
)
947 constexpr bitcount_t xtypebits
= bitcount_t(sizeof(xtype
) * 8);
948 constexpr bitcount_t bits
= bitcount_t(sizeof(itype
) * 8);
949 constexpr bitcount_t opbits
= xtypebits
>= 128 ? 6
950 : xtypebits
>= 64 ? 5
951 : xtypebits
>= 32 ? 4
952 : xtypebits
>= 16 ? 3
954 constexpr bitcount_t shift
= bits
- xtypebits
;
955 constexpr bitcount_t mask
= (1 << opbits
) - 1;
956 bitcount_t rshift
= opbits
? (internal
>> (bits
- opbits
)) & mask
: 0;
957 internal
^= internal
>> (opbits
+ rshift
);
958 internal
*= mcg_multiplier
<itype
>::multiplier();
959 xtype result
= internal
>> shift
;
965 * XSL RR -- fixed xorshift (to low bits), random rotate
967 * Useful for 128-bit types that are split across two CPU registers.
970 template <typename xtype
, typename itype
>
971 struct xsl_rr_mixin
{
972 static xtype
output(itype internal
)
974 constexpr bitcount_t xtypebits
= bitcount_t(sizeof(xtype
) * 8);
975 constexpr bitcount_t bits
= bitcount_t(sizeof(itype
) * 8);
976 constexpr bitcount_t sparebits
= bits
- xtypebits
;
977 constexpr bitcount_t wantedopbits
= xtypebits
>= 128 ? 7
978 : xtypebits
>= 64 ? 6
979 : xtypebits
>= 32 ? 5
980 : xtypebits
>= 16 ? 4
982 constexpr bitcount_t opbits
= sparebits
>= wantedopbits
? wantedopbits
984 constexpr bitcount_t amplifier
= wantedopbits
- opbits
;
985 constexpr bitcount_t mask
= (1 << opbits
) - 1;
986 constexpr bitcount_t topspare
= sparebits
;
987 constexpr bitcount_t bottomspare
= sparebits
- topspare
;
988 constexpr bitcount_t xshift
= (topspare
+ xtypebits
) / 2;
991 opbits
? bitcount_t(internal
>> (bits
- opbits
)) & mask
: 0;
992 bitcount_t amprot
= (rot
<< amplifier
) & mask
;
993 internal
^= internal
>> xshift
;
994 xtype result
= xtype(internal
>> bottomspare
);
995 result
= rotr(result
, amprot
);
1002 * XSL RR RR -- fixed xorshift (to low bits), random rotate (both parts)
1004 * Useful for 128-bit types that are split across two CPU registers.
1005 * If you really want an invertable 128-bit RNG, I guess this is the one.
1008 template <typename T
> struct halfsize_trait
{};
1009 template <> struct halfsize_trait
<pcg128_t
> { typedef uint64_t type
; };
1010 template <> struct halfsize_trait
<uint64_t> { typedef uint32_t type
; };
1011 template <> struct halfsize_trait
<uint32_t> { typedef uint16_t type
; };
1012 template <> struct halfsize_trait
<uint16_t> { typedef uint8_t type
; };
1014 template <typename xtype
, typename itype
>
1015 struct xsl_rr_rr_mixin
{
1016 typedef typename halfsize_trait
<itype
>::type htype
;
1018 static itype
output(itype internal
)
1020 constexpr bitcount_t htypebits
= bitcount_t(sizeof(htype
) * 8);
1021 constexpr bitcount_t bits
= bitcount_t(sizeof(itype
) * 8);
1022 constexpr bitcount_t sparebits
= bits
- htypebits
;
1023 constexpr bitcount_t wantedopbits
= htypebits
>= 128 ? 7
1024 : htypebits
>= 64 ? 6
1025 : htypebits
>= 32 ? 5
1026 : htypebits
>= 16 ? 4
1028 constexpr bitcount_t opbits
= sparebits
>= wantedopbits
? wantedopbits
1030 constexpr bitcount_t amplifier
= wantedopbits
- opbits
;
1031 constexpr bitcount_t mask
= (1 << opbits
) - 1;
1032 constexpr bitcount_t topspare
= sparebits
;
1033 constexpr bitcount_t xshift
= (topspare
+ htypebits
) / 2;
1036 opbits
? bitcount_t(internal
>> (bits
- opbits
)) & mask
: 0;
1037 bitcount_t amprot
= (rot
<< amplifier
) & mask
;
1038 internal
^= internal
>> xshift
;
1039 htype lowbits
= htype(internal
);
1040 lowbits
= rotr(lowbits
, amprot
);
1041 htype highbits
= htype(internal
>> topspare
);
1042 bitcount_t rot2
= lowbits
& mask
;
1043 bitcount_t amprot2
= (rot2
<< amplifier
) & mask
;
1044 highbits
= rotr(highbits
, amprot2
);
1045 return (itype(highbits
) << topspare
) ^ itype(lowbits
);
1051 * XSH -- fixed xorshift (to high bits)
1053 * You shouldn't use this at 64-bits or less.
1056 template <typename xtype
, typename itype
>
1058 static xtype
output(itype internal
)
1060 constexpr bitcount_t xtypebits
= bitcount_t(sizeof(xtype
) * 8);
1061 constexpr bitcount_t bits
= bitcount_t(sizeof(itype
) * 8);
1062 constexpr bitcount_t sparebits
= bits
- xtypebits
;
1063 constexpr bitcount_t topspare
= 0;
1064 constexpr bitcount_t bottomspare
= sparebits
- topspare
;
1065 constexpr bitcount_t xshift
= (topspare
+ xtypebits
) / 2;
1067 internal
^= internal
>> xshift
;
1068 xtype result
= internal
>> bottomspare
;
1074 * XSL -- fixed xorshift (to low bits)
1076 * You shouldn't use this at 64-bits or less.
1079 template <typename xtype
, typename itype
>
1081 inline xtype
output(itype internal
)
1083 constexpr bitcount_t xtypebits
= bitcount_t(sizeof(xtype
) * 8);
1084 constexpr bitcount_t bits
= bitcount_t(sizeof(itype
) * 8);
1085 constexpr bitcount_t sparebits
= bits
- xtypebits
;
1086 constexpr bitcount_t topspare
= sparebits
;
1087 constexpr bitcount_t bottomspare
= sparebits
- topspare
;
1088 constexpr bitcount_t xshift
= (topspare
+ xtypebits
) / 2;
1090 internal
^= internal
>> xshift
;
1091 xtype result
= internal
>> bottomspare
;
1096 /* ---- End of Output Functions ---- */
1099 template <typename baseclass
>
1100 struct inside_out
: private baseclass
{
1101 inside_out() = delete;
1103 typedef typename
baseclass::result_type result_type
;
1104 typedef typename
baseclass::state_type state_type
;
1105 static_assert(sizeof(result_type
) == sizeof(state_type
),
1106 "Require a RNG whose output function is a permutation");
1108 static bool external_step(result_type
& randval
, size_t i
)
1110 state_type state
= baseclass::unoutput(randval
);
1111 state
= state
* baseclass::multiplier() + baseclass::increment()
1113 result_type result
= baseclass::output(state
);
1116 baseclass::is_mcg
? state
& state_type(3U) : state_type(0U);
1117 return result
== zero
;
1120 static bool external_advance(result_type
& randval
, size_t i
,
1121 result_type delta
, bool forwards
= true)
1123 state_type state
= baseclass::unoutput(randval
);
1124 state_type mult
= baseclass::multiplier();
1125 state_type inc
= baseclass::increment() + state_type(i
*2);
1127 baseclass::is_mcg
? state
& state_type(3U) : state_type(0U);
1128 state_type dist_to_zero
= baseclass::distance(state
, zero
, mult
, inc
);
1130 forwards
? dist_to_zero
<= delta
1131 : (-dist_to_zero
) <= delta
;
1134 state
= baseclass::advance(state
, delta
, mult
, inc
);
1135 randval
= baseclass::output(state
);
1136 return crosses_zero
;
1141 template <bitcount_t table_pow2
, bitcount_t advance_pow2
, typename baseclass
, typename extvalclass
, bool kdd
= true>
1142 class extended
: public baseclass
{
1144 typedef typename
baseclass::state_type state_type
;
1145 typedef typename
baseclass::result_type result_type
;
1146 typedef inside_out
<extvalclass
> insideout
;
1149 static constexpr bitcount_t rtypebits
= sizeof(result_type
)*8;
1150 static constexpr bitcount_t stypebits
= sizeof(state_type
)*8;
1152 static constexpr bitcount_t tick_limit_pow2
= 64U;
1154 static constexpr size_t table_size
= 1UL << table_pow2
;
1155 static constexpr size_t table_shift
= stypebits
- table_pow2
;
1156 static constexpr state_type table_mask
=
1157 (state_type(1U) << table_pow2
) - state_type(1U);
1159 static constexpr bool may_tick
=
1160 (advance_pow2
< stypebits
) && (advance_pow2
< tick_limit_pow2
);
1161 static constexpr size_t tick_shift
= stypebits
- advance_pow2
;
1162 static constexpr state_type tick_mask
=
1163 may_tick
? state_type(
1164 (uint64_t(1) << (advance_pow2
*may_tick
)) - 1)
1165 // ^-- stupidity to appease GCC warnings
1168 static constexpr bool may_tock
= stypebits
< tick_limit_pow2
;
1170 result_type data_
[table_size
];
1172 PCG_NOINLINE
void advance_table();
1174 PCG_NOINLINE
void advance_table(state_type delta
, bool isForwards
= true);
1176 result_type
& get_extended_value()
1178 state_type state
= this->state_
;
1179 if (kdd
&& baseclass::is_mcg
) {
1180 // The low order bits of an MCG are constant, so drop them.
1183 size_t index
= kdd
? state
& table_mask
1184 : state
>> table_shift
;
1187 bool tick
= kdd
? (state
& tick_mask
) == state_type(0u)
1188 : (state
>> tick_shift
) == state_type(0u);
1193 bool tock
= state
== state_type(0u);
1197 return data_
[index
];
1201 static constexpr size_t period_pow2()
1203 return baseclass::period_pow2() + table_size
*extvalclass::period_pow2();
1206 __attribute__((always_inline
)) result_type
operator()()
1208 result_type rhs
= get_extended_value();
1209 result_type lhs
= this->baseclass::operator()();
1213 result_type
operator()(result_type upper_bound
)
1215 return bounded_rand(*this, upper_bound
);
1218 void set(result_type wanted
)
1220 result_type
& rhs
= get_extended_value();
1221 result_type lhs
= this->baseclass::operator()();
1225 void advance(state_type distance
, bool forwards
= true);
1227 void backstep(state_type distance
)
1229 advance(distance
, false);
1232 extended(const result_type
* data
)
1238 extended(const result_type
* data
, state_type seed
)
1244 // This function may or may not exist. It thus has to be a template
1245 // to use SFINAE; users don't have to worry about its template-ness.
1247 template <typename bc
= baseclass
>
1248 extended(const result_type
* data
, state_type seed
,
1249 typename
bc::stream_state stream_seed
)
1250 : baseclass(seed
, stream_seed
)
1261 extended(state_type seed
)
1267 // This function may or may not exist. It thus has to be a template
1268 // to use SFINAE; users don't have to worry about its template-ness.
1270 template <typename bc
= baseclass
>
1271 extended(state_type seed
, typename
bc::stream_state stream_seed
)
1272 : baseclass(seed
, stream_seed
)
1279 void datainit(const result_type
* data
);
1283 template<typename SeedSeq
, typename
= typename
std::enable_if
<
1284 !std::is_convertible
<SeedSeq
, result_type
>::value
1285 && !std::is_convertible
<SeedSeq
, extended
>::value
>::type
>
1286 extended(SeedSeq
&& seedSeq
)
1287 : baseclass(seedSeq
)
1289 generate_to
<table_size
>(seedSeq
, data_
);
1292 template<typename
... Args
>
1293 void seed(Args
&&... args
)
1295 new (this) extended(std::forward
<Args
>(args
)...);
1298 template <bitcount_t table_pow2_
, bitcount_t advance_pow2_
,
1299 typename baseclass_
, typename extvalclass_
, bool kdd_
>
1300 friend bool operator==(const extended
<table_pow2_
, advance_pow2_
,
1301 baseclass_
, extvalclass_
, kdd_
>&,
1302 const extended
<table_pow2_
, advance_pow2_
,
1303 baseclass_
, extvalclass_
, kdd_
>&);
1305 template <typename CharT
, typename Traits
,
1306 bitcount_t table_pow2_
, bitcount_t advance_pow2_
,
1307 typename baseclass_
, typename extvalclass_
, bool kdd_
>
1308 friend std::basic_ostream
<CharT
,Traits
>&
1309 operator<<(std::basic_ostream
<CharT
,Traits
>& out
,
1310 const extended
<table_pow2_
, advance_pow2_
,
1311 baseclass_
, extvalclass_
, kdd_
>&);
1313 template <typename CharT
, typename Traits
,
1314 bitcount_t table_pow2_
, bitcount_t advance_pow2_
,
1315 typename baseclass_
, typename extvalclass_
, bool kdd_
>
1316 friend std::basic_istream
<CharT
,Traits
>&
1317 operator>>(std::basic_istream
<CharT
,Traits
>& in
,
1318 extended
<table_pow2_
, advance_pow2_
,
1319 baseclass_
, extvalclass_
, kdd_
>&);
1324 template <bitcount_t table_pow2
, bitcount_t advance_pow2
,
1325 typename baseclass
, typename extvalclass
, bool kdd
>
1326 void extended
<table_pow2
,advance_pow2
,baseclass
,extvalclass
,kdd
>::datainit(
1327 const result_type
* data
)
1329 for (size_t i
= 0; i
< table_size
; ++i
)
1333 template <bitcount_t table_pow2
, bitcount_t advance_pow2
,
1334 typename baseclass
, typename extvalclass
, bool kdd
>
1335 void extended
<table_pow2
,advance_pow2
,baseclass
,extvalclass
,kdd
>::selfinit()
1337 // We need to fill the extended table with something, and we have
1338 // very little provided data, so we use the base generator to
1339 // produce values. Although not ideal (use a seed sequence, folks!),
1340 // unexpected correlations are mitigated by
1341 // - using XOR differences rather than the number directly
1342 // - the way the table is accessed, its values *won't* be accessed
1343 // in the same order the were written.
1344 // - any strange correlations would only be apparent if we
1345 // were to backstep the generator so that the base generator
1346 // was generating the same values again
1347 result_type xdiff
= baseclass::operator()() - baseclass::operator()();
1348 for (size_t i
= 0; i
< table_size
; ++i
) {
1349 data_
[i
] = baseclass::operator()() ^ xdiff
;
1353 template <bitcount_t table_pow2
, bitcount_t advance_pow2
,
1354 typename baseclass
, typename extvalclass
, bool kdd
>
1355 bool operator==(const extended
<table_pow2
, advance_pow2
,
1356 baseclass
, extvalclass
, kdd
>& lhs
,
1357 const extended
<table_pow2
, advance_pow2
,
1358 baseclass
, extvalclass
, kdd
>& rhs
)
1360 auto& base_lhs
= static_cast<const baseclass
&>(lhs
);
1361 auto& base_rhs
= static_cast<const baseclass
&>(rhs
);
1362 return base_lhs
== base_rhs
1363 && !memcmp((void*) lhs
.data_
, (void*) rhs
.data_
, sizeof(lhs
.data_
));
1366 template <bitcount_t table_pow2
, bitcount_t advance_pow2
,
1367 typename baseclass
, typename extvalclass
, bool kdd
>
1368 inline bool operator!=(const extended
<table_pow2
, advance_pow2
,
1369 baseclass
, extvalclass
, kdd
>& lhs
,
1370 const extended
<table_pow2
, advance_pow2
,
1371 baseclass
, extvalclass
, kdd
>& rhs
)
1376 template <typename CharT
, typename Traits
,
1377 bitcount_t table_pow2
, bitcount_t advance_pow2
,
1378 typename baseclass
, typename extvalclass
, bool kdd
>
1379 std::basic_ostream
<CharT
,Traits
>&
1380 operator<<(std::basic_ostream
<CharT
,Traits
>& out
,
1381 const extended
<table_pow2
, advance_pow2
,
1382 baseclass
, extvalclass
, kdd
>& rng
)
1384 auto orig_flags
= out
.flags(std::ios_base::dec
| std::ios_base::left
);
1385 auto space
= out
.widen(' ');
1386 auto orig_fill
= out
.fill();
1388 out
<< rng
.multiplier() << space
1389 << rng
.increment() << space
1392 for (const auto& datum
: rng
.data_
)
1393 out
<< space
<< datum
;
1395 out
.flags(orig_flags
);
1396 out
.fill(orig_fill
);
1400 template <typename CharT
, typename Traits
,
1401 bitcount_t table_pow2
, bitcount_t advance_pow2
,
1402 typename baseclass
, typename extvalclass
, bool kdd
>
1403 std::basic_istream
<CharT
,Traits
>&
1404 operator>>(std::basic_istream
<CharT
,Traits
>& in
,
1405 extended
<table_pow2
, advance_pow2
,
1406 baseclass
, extvalclass
, kdd
>& rng
)
1408 extended
<table_pow2
, advance_pow2
, baseclass
, extvalclass
> new_rng
;
1409 auto& base_rng
= static_cast<baseclass
&>(new_rng
);
1415 auto orig_flags
= in
.flags(std::ios_base::dec
| std::ios_base::skipws
);
1417 for (auto& datum
: new_rng
.data_
) {
1426 in
.flags(orig_flags
);
1432 template <bitcount_t table_pow2
, bitcount_t advance_pow2
,
1433 typename baseclass
, typename extvalclass
, bool kdd
>
1435 extended
<table_pow2
,advance_pow2
,baseclass
,extvalclass
,kdd
>::advance_table()
1438 for (size_t i
= 0; i
< table_size
; ++i
) {
1440 carry
= insideout::external_step(data_
[i
],i
+1);
1442 bool carry2
= insideout::external_step(data_
[i
],i
+1);
1443 carry
= carry
|| carry2
;
1447 template <bitcount_t table_pow2
, bitcount_t advance_pow2
,
1448 typename baseclass
, typename extvalclass
, bool kdd
>
1450 extended
<table_pow2
,advance_pow2
,baseclass
,extvalclass
,kdd
>::advance_table(
1451 state_type delta
, bool isForwards
)
1453 typedef typename
baseclass::state_type base_state_t
;
1454 typedef typename
extvalclass::state_type ext_state_t
;
1455 constexpr bitcount_t basebits
= sizeof(base_state_t
)*8;
1456 constexpr bitcount_t extbits
= sizeof(ext_state_t
)*8;
1457 static_assert(basebits
<= extbits
|| advance_pow2
> 0,
1458 "Current implementation might overflow its carry");
1460 base_state_t carry
= 0;
1461 for (size_t i
= 0; i
< table_size
; ++i
) {
1462 base_state_t total_delta
= carry
+ delta
;
1463 ext_state_t trunc_delta
= ext_state_t(total_delta
);
1464 if (basebits
> extbits
) {
1465 carry
= total_delta
>> extbits
;
1470 insideout::external_advance(data_
[i
],i
+1, trunc_delta
, isForwards
);
1474 template <bitcount_t table_pow2
, bitcount_t advance_pow2
,
1475 typename baseclass
, typename extvalclass
, bool kdd
>
1476 void extended
<table_pow2
,advance_pow2
,baseclass
,extvalclass
,kdd
>::advance(
1477 state_type distance
, bool forwards
)
1480 "Efficient advance is too hard for non-kdd extension. "
1481 "For a weak advance, cast to base class");
1483 baseclass::is_mcg
? this->state_
& state_type(3U) : state_type(0U);
1485 state_type ticks
= distance
>> (advance_pow2
*may_tick
);
1486 // ^-- stupidity to appease GCC
1488 state_type adv_mask
=
1489 baseclass::is_mcg
? tick_mask
<< 2 : tick_mask
;
1490 state_type next_advance_distance
= this->distance(zero
, adv_mask
);
1492 next_advance_distance
= (-next_advance_distance
) & tick_mask
;
1493 if (next_advance_distance
< (distance
& tick_mask
)) {
1497 advance_table(ticks
, forwards
);
1500 if (may_tock
&& this->distance(zero
) <= distance
)
1502 baseclass::advance(distance
);
1504 if (may_tock
&& -(this->distance(zero
)) <= distance
)
1505 advance_table(state_type(1U), false);
1506 baseclass::advance(-distance
);
1510 } // namespace pcg_detail
1512 namespace pcg_engines
{
1514 using namespace pcg_detail
;
1516 /* Predefined types for XSH RS */
1518 typedef oneseq_base
<uint8_t, uint16_t, xsh_rs_mixin
> oneseq_xsh_rs_16_8
;
1519 typedef oneseq_base
<uint16_t, uint32_t, xsh_rs_mixin
> oneseq_xsh_rs_32_16
;
1520 typedef oneseq_base
<uint32_t, uint64_t, xsh_rs_mixin
> oneseq_xsh_rs_64_32
;
1521 typedef oneseq_base
<uint64_t, pcg128_t
, xsh_rs_mixin
> oneseq_xsh_rs_128_64
;
1523 typedef unique_base
<uint8_t, uint16_t, xsh_rs_mixin
> unique_xsh_rs_16_8
;
1524 typedef unique_base
<uint16_t, uint32_t, xsh_rs_mixin
> unique_xsh_rs_32_16
;
1525 typedef unique_base
<uint32_t, uint64_t, xsh_rs_mixin
> unique_xsh_rs_64_32
;
1526 typedef unique_base
<uint64_t, pcg128_t
, xsh_rs_mixin
> unique_xsh_rs_128_64
;
1528 typedef setseq_base
<uint8_t, uint16_t, xsh_rs_mixin
> setseq_xsh_rs_16_8
;
1529 typedef setseq_base
<uint16_t, uint32_t, xsh_rs_mixin
> setseq_xsh_rs_32_16
;
1530 typedef setseq_base
<uint32_t, uint64_t, xsh_rs_mixin
> setseq_xsh_rs_64_32
;
1531 typedef setseq_base
<uint64_t, pcg128_t
, xsh_rs_mixin
> setseq_xsh_rs_128_64
;
1533 typedef mcg_base
<uint8_t, uint16_t, xsh_rs_mixin
> mcg_xsh_rs_16_8
;
1534 typedef mcg_base
<uint16_t, uint32_t, xsh_rs_mixin
> mcg_xsh_rs_32_16
;
1535 typedef mcg_base
<uint32_t, uint64_t, xsh_rs_mixin
> mcg_xsh_rs_64_32
;
1536 typedef mcg_base
<uint64_t, pcg128_t
, xsh_rs_mixin
> mcg_xsh_rs_128_64
;
1538 /* Predefined types for XSH RR */
1540 typedef oneseq_base
<uint8_t, uint16_t, xsh_rr_mixin
> oneseq_xsh_rr_16_8
;
1541 typedef oneseq_base
<uint16_t, uint32_t, xsh_rr_mixin
> oneseq_xsh_rr_32_16
;
1542 typedef oneseq_base
<uint32_t, uint64_t, xsh_rr_mixin
> oneseq_xsh_rr_64_32
;
1543 typedef oneseq_base
<uint64_t, pcg128_t
, xsh_rr_mixin
> oneseq_xsh_rr_128_64
;
1545 typedef unique_base
<uint8_t, uint16_t, xsh_rr_mixin
> unique_xsh_rr_16_8
;
1546 typedef unique_base
<uint16_t, uint32_t, xsh_rr_mixin
> unique_xsh_rr_32_16
;
1547 typedef unique_base
<uint32_t, uint64_t, xsh_rr_mixin
> unique_xsh_rr_64_32
;
1548 typedef unique_base
<uint64_t, pcg128_t
, xsh_rr_mixin
> unique_xsh_rr_128_64
;
1550 typedef setseq_base
<uint8_t, uint16_t, xsh_rr_mixin
> setseq_xsh_rr_16_8
;
1551 typedef setseq_base
<uint16_t, uint32_t, xsh_rr_mixin
> setseq_xsh_rr_32_16
;
1552 typedef setseq_base
<uint32_t, uint64_t, xsh_rr_mixin
> setseq_xsh_rr_64_32
;
1553 typedef setseq_base
<uint64_t, pcg128_t
, xsh_rr_mixin
> setseq_xsh_rr_128_64
;
1555 typedef mcg_base
<uint8_t, uint16_t, xsh_rr_mixin
> mcg_xsh_rr_16_8
;
1556 typedef mcg_base
<uint16_t, uint32_t, xsh_rr_mixin
> mcg_xsh_rr_32_16
;
1557 typedef mcg_base
<uint32_t, uint64_t, xsh_rr_mixin
> mcg_xsh_rr_64_32
;
1558 typedef mcg_base
<uint64_t, pcg128_t
, xsh_rr_mixin
> mcg_xsh_rr_128_64
;
1561 /* Predefined types for RXS M XS */
1563 typedef oneseq_base
<uint8_t, uint8_t, rxs_m_xs_mixin
> oneseq_rxs_m_xs_8_8
;
1564 typedef oneseq_base
<uint16_t, uint16_t, rxs_m_xs_mixin
> oneseq_rxs_m_xs_16_16
;
1565 typedef oneseq_base
<uint32_t, uint32_t, rxs_m_xs_mixin
> oneseq_rxs_m_xs_32_32
;
1566 typedef oneseq_base
<uint64_t, uint64_t, rxs_m_xs_mixin
> oneseq_rxs_m_xs_64_64
;
1567 typedef oneseq_base
<pcg128_t
, pcg128_t
, rxs_m_xs_mixin
> oneseq_rxs_m_xs_128_128
;
1569 typedef unique_base
<uint8_t, uint8_t, rxs_m_xs_mixin
> unique_rxs_m_xs_8_8
;
1570 typedef unique_base
<uint16_t, uint16_t, rxs_m_xs_mixin
> unique_rxs_m_xs_16_16
;
1571 typedef unique_base
<uint32_t, uint32_t, rxs_m_xs_mixin
> unique_rxs_m_xs_32_32
;
1572 typedef unique_base
<uint64_t, uint64_t, rxs_m_xs_mixin
> unique_rxs_m_xs_64_64
;
1573 typedef unique_base
<pcg128_t
, pcg128_t
, rxs_m_xs_mixin
> unique_rxs_m_xs_128_128
;
1575 typedef setseq_base
<uint8_t, uint8_t, rxs_m_xs_mixin
> setseq_rxs_m_xs_8_8
;
1576 typedef setseq_base
<uint16_t, uint16_t, rxs_m_xs_mixin
> setseq_rxs_m_xs_16_16
;
1577 typedef setseq_base
<uint32_t, uint32_t, rxs_m_xs_mixin
> setseq_rxs_m_xs_32_32
;
1578 typedef setseq_base
<uint64_t, uint64_t, rxs_m_xs_mixin
> setseq_rxs_m_xs_64_64
;
1579 typedef setseq_base
<pcg128_t
, pcg128_t
, rxs_m_xs_mixin
> setseq_rxs_m_xs_128_128
;
1581 // MCG versions don't make sense here, so aren't defined.
1583 /* Predefined types for XSL RR (only defined for "large" types) */
1585 typedef oneseq_base
<uint32_t, uint64_t, xsl_rr_mixin
> oneseq_xsl_rr_64_32
;
1586 typedef oneseq_base
<uint64_t, pcg128_t
, xsl_rr_mixin
> oneseq_xsl_rr_128_64
;
1588 typedef unique_base
<uint32_t, uint64_t, xsl_rr_mixin
> unique_xsl_rr_64_32
;
1589 typedef unique_base
<uint64_t, pcg128_t
, xsl_rr_mixin
> unique_xsl_rr_128_64
;
1591 typedef setseq_base
<uint32_t, uint64_t, xsl_rr_mixin
> setseq_xsl_rr_64_32
;
1592 typedef setseq_base
<uint64_t, pcg128_t
, xsl_rr_mixin
> setseq_xsl_rr_128_64
;
1594 typedef mcg_base
<uint32_t, uint64_t, xsl_rr_mixin
> mcg_xsl_rr_64_32
;
1595 typedef mcg_base
<uint64_t, pcg128_t
, xsl_rr_mixin
> mcg_xsl_rr_128_64
;
1598 /* Predefined types for XSL RR RR (only defined for "large" types) */
1600 typedef oneseq_base
<uint64_t, uint64_t, xsl_rr_rr_mixin
>
1601 oneseq_xsl_rr_rr_64_64
;
1602 typedef oneseq_base
<pcg128_t
, pcg128_t
, xsl_rr_rr_mixin
>
1603 oneseq_xsl_rr_rr_128_128
;
1605 typedef unique_base
<uint64_t, uint64_t, xsl_rr_rr_mixin
>
1606 unique_xsl_rr_rr_64_64
;
1607 typedef unique_base
<pcg128_t
, pcg128_t
, xsl_rr_rr_mixin
>
1608 unique_xsl_rr_rr_128_128
;
1610 typedef setseq_base
<uint64_t, uint64_t, xsl_rr_rr_mixin
>
1611 setseq_xsl_rr_rr_64_64
;
1612 typedef setseq_base
<pcg128_t
, pcg128_t
, xsl_rr_rr_mixin
>
1613 setseq_xsl_rr_rr_128_128
;
1615 // MCG versions don't make sense here, so aren't defined.
1617 /* Extended generators */
1619 template <bitcount_t table_pow2
, bitcount_t advance_pow2
,
1620 typename BaseRNG
, bool kdd
= true>
1621 using ext_std8
= extended
<table_pow2
, advance_pow2
, BaseRNG
,
1622 oneseq_rxs_m_xs_8_8
, kdd
>;
1624 template <bitcount_t table_pow2
, bitcount_t advance_pow2
,
1625 typename BaseRNG
, bool kdd
= true>
1626 using ext_std16
= extended
<table_pow2
, advance_pow2
, BaseRNG
,
1627 oneseq_rxs_m_xs_16_16
, kdd
>;
1629 template <bitcount_t table_pow2
, bitcount_t advance_pow2
,
1630 typename BaseRNG
, bool kdd
= true>
1631 using ext_std32
= extended
<table_pow2
, advance_pow2
, BaseRNG
,
1632 oneseq_rxs_m_xs_32_32
, kdd
>;
1634 template <bitcount_t table_pow2
, bitcount_t advance_pow2
,
1635 typename BaseRNG
, bool kdd
= true>
1636 using ext_std64
= extended
<table_pow2
, advance_pow2
, BaseRNG
,
1637 oneseq_rxs_m_xs_64_64
, kdd
>;
1640 template <bitcount_t table_pow2
, bitcount_t advance_pow2
, bool kdd
= true>
1641 using ext_oneseq_rxs_m_xs_32_32
=
1642 ext_std32
<table_pow2
, advance_pow2
, oneseq_rxs_m_xs_32_32
, kdd
>;
1644 template <bitcount_t table_pow2
, bitcount_t advance_pow2
, bool kdd
= true>
1645 using ext_mcg_xsh_rs_64_32
=
1646 ext_std32
<table_pow2
, advance_pow2
, mcg_xsh_rs_64_32
, kdd
>;
1648 template <bitcount_t table_pow2
, bitcount_t advance_pow2
, bool kdd
= true>
1649 using ext_oneseq_xsh_rs_64_32
=
1650 ext_std32
<table_pow2
, advance_pow2
, oneseq_xsh_rs_64_32
, kdd
>;
1652 template <bitcount_t table_pow2
, bitcount_t advance_pow2
, bool kdd
= true>
1653 using ext_setseq_xsh_rr_64_32
=
1654 ext_std32
<table_pow2
, advance_pow2
, setseq_xsh_rr_64_32
, kdd
>;
1656 template <bitcount_t table_pow2
, bitcount_t advance_pow2
, bool kdd
= true>
1657 using ext_mcg_xsl_rr_128_64
=
1658 ext_std64
<table_pow2
, advance_pow2
, mcg_xsl_rr_128_64
, kdd
>;
1660 template <bitcount_t table_pow2
, bitcount_t advance_pow2
, bool kdd
= true>
1661 using ext_oneseq_xsl_rr_128_64
=
1662 ext_std64
<table_pow2
, advance_pow2
, oneseq_xsl_rr_128_64
, kdd
>;
1664 template <bitcount_t table_pow2
, bitcount_t advance_pow2
, bool kdd
= true>
1665 using ext_setseq_xsl_rr_128_64
=
1666 ext_std64
<table_pow2
, advance_pow2
, setseq_xsl_rr_128_64
, kdd
>;
1668 } // namespace pcg_engines
1670 typedef pcg_engines::setseq_xsh_rr_64_32 pcg32
;
1671 typedef pcg_engines::oneseq_xsh_rr_64_32 pcg32_oneseq
;
1672 typedef pcg_engines::unique_xsh_rr_64_32 pcg32_unique
;
1673 typedef pcg_engines::mcg_xsh_rs_64_32 pcg32_fast
;
1675 typedef pcg_engines::setseq_xsl_rr_128_64 pcg64
;
1676 typedef pcg_engines::oneseq_xsl_rr_128_64 pcg64_oneseq
;
1677 typedef pcg_engines::unique_xsl_rr_128_64 pcg64_unique
;
1678 typedef pcg_engines::mcg_xsl_rr_128_64 pcg64_fast
;
1680 typedef pcg_engines::setseq_rxs_m_xs_8_8 pcg8_once_insecure
;
1681 typedef pcg_engines::setseq_rxs_m_xs_16_16 pcg16_once_insecure
;
1682 typedef pcg_engines::setseq_rxs_m_xs_32_32 pcg32_once_insecure
;
1683 typedef pcg_engines::setseq_rxs_m_xs_64_64 pcg64_once_insecure
;
1684 typedef pcg_engines::setseq_xsl_rr_rr_128_128 pcg128_once_insecure
;
1686 typedef pcg_engines::oneseq_rxs_m_xs_8_8 pcg8_oneseq_once_insecure
;
1687 typedef pcg_engines::oneseq_rxs_m_xs_16_16 pcg16_oneseq_once_insecure
;
1688 typedef pcg_engines::oneseq_rxs_m_xs_32_32 pcg32_oneseq_once_insecure
;
1689 typedef pcg_engines::oneseq_rxs_m_xs_64_64 pcg64_oneseq_once_insecure
;
1690 typedef pcg_engines::oneseq_xsl_rr_rr_128_128 pcg128_oneseq_once_insecure
;
1693 // These two extended RNGs provide two-dimensionally equidistributed
1694 // 32-bit generators. pcg32_k2_fast occupies the same space as pcg64,
1695 // and can be called twice to generate 64 bits, but does not required
1696 // 128-bit math; on 32-bit systems, it's faster than pcg64 as well.
1698 typedef pcg_engines::ext_setseq_xsh_rr_64_32
<6,16,true> pcg32_k2
;
1699 typedef pcg_engines::ext_oneseq_xsh_rs_64_32
<6,32,true> pcg32_k2_fast
;
1701 // These eight extended RNGs have about as much state as arc4random
1703 // - the k variants are k-dimensionally equidistributed
1704 // - the c variants offer better crypographic security
1706 // (just how good the cryptographic security is is an open question)
1708 typedef pcg_engines::ext_setseq_xsh_rr_64_32
<6,16,true> pcg32_k64
;
1709 typedef pcg_engines::ext_mcg_xsh_rs_64_32
<6,32,true> pcg32_k64_oneseq
;
1710 typedef pcg_engines::ext_oneseq_xsh_rs_64_32
<6,32,true> pcg32_k64_fast
;
1712 typedef pcg_engines::ext_setseq_xsh_rr_64_32
<6,16,false> pcg32_c64
;
1713 typedef pcg_engines::ext_oneseq_xsh_rs_64_32
<6,32,false> pcg32_c64_oneseq
;
1714 typedef pcg_engines::ext_mcg_xsh_rs_64_32
<6,32,false> pcg32_c64_fast
;
1716 typedef pcg_engines::ext_setseq_xsl_rr_128_64
<5,16,true> pcg64_k32
;
1717 typedef pcg_engines::ext_oneseq_xsl_rr_128_64
<5,128,true> pcg64_k32_oneseq
;
1718 typedef pcg_engines::ext_mcg_xsl_rr_128_64
<5,128,true> pcg64_k32_fast
;
1720 typedef pcg_engines::ext_setseq_xsl_rr_128_64
<5,16,false> pcg64_c32
;
1721 typedef pcg_engines::ext_oneseq_xsl_rr_128_64
<5,128,false> pcg64_c32_oneseq
;
1722 typedef pcg_engines::ext_mcg_xsl_rr_128_64
<5,128,false> pcg64_c32_fast
;
1724 // These eight extended RNGs have more state than the Mersenne twister
1726 // - the k variants are k-dimensionally equidistributed
1727 // - the c variants offer better crypographic security
1729 // (just how good the cryptographic security is is an open question)
1731 typedef pcg_engines::ext_setseq_xsh_rr_64_32
<10,16,true> pcg32_k1024
;
1732 typedef pcg_engines::ext_oneseq_xsh_rs_64_32
<10,32,true> pcg32_k1024_fast
;
1734 typedef pcg_engines::ext_setseq_xsh_rr_64_32
<10,16,false> pcg32_c1024
;
1735 typedef pcg_engines::ext_oneseq_xsh_rs_64_32
<10,32,false> pcg32_c1024_fast
;
1737 typedef pcg_engines::ext_setseq_xsl_rr_128_64
<10,16,true> pcg64_k1024
;
1738 typedef pcg_engines::ext_oneseq_xsl_rr_128_64
<10,128,true> pcg64_k1024_fast
;
1740 typedef pcg_engines::ext_setseq_xsl_rr_128_64
<10,16,false> pcg64_c1024
;
1741 typedef pcg_engines::ext_oneseq_xsl_rr_128_64
<10,128,false> pcg64_c1024_fast
;
1743 // These generators have an insanely huge period (2^524352), and is suitable
1744 // for silly party tricks, such as dumping out 64 KB ZIP files at an arbitrary
1745 // point in the future. [Actually, over the full period of the generator, it
1746 // will produce every 64 KB ZIP file 2^64 times!]
1748 typedef pcg_engines::ext_setseq_xsh_rr_64_32
<14,16,true> pcg32_k16384
;
1749 typedef pcg_engines::ext_oneseq_xsh_rs_64_32
<14,32,true> pcg32_k16384_fast
;
1751 #endif // PCG_RAND_HPP_INCLUDED