prrng 1.12.1
Loading...
Searching...
No Matches
prrng::pcg32 Class Reference

Random number generate using the pcg32 algorithm. More...

#include <prrng.h>

Inheritance diagram for prrng::pcg32:
prrng::GeneratorBase< pcg32 > prrng::pcg32_index

Public Member Functions

template<typename T = uint64_t, typename S = uint64_t>
 pcg32 (T initstate=0x853c49e6748fea9bULL, S initseq=0xda3e39cb94b95bdbULL)
 Constructor.
 
void seed (uint64_t initstate=0x853c49e6748fea9bULL, uint64_t initseq=0xda3e39cb94b95bdbULL)
 Seed the generator (constructor alias).
 
uint32_t operator() ()
 Draw new random number (uniformly distributed, 0 <= r <= max(uint32_t)).
 
uint32_t next_uint32 ()
 Draw new random number (uniformly distributed, 0 <= r <= max(uint32_t)).
 
uint32_t next_uint32 (uint32_t bound)
 Draw new random number (uniformly distributed, 0 <= r <= bound).
 
float next_float ()
 Generate a single precision floating point value on the interval [0, 1).
 
double next_double ()
 Generate a double precision floating point value on the interval [0, 1).
 
double next_positive_double ()
 Generate a double precision floating point value on the interval (0, 1).
 
uint64_t state () const
 The current "state" of the generator.
 
template<typename R >
state ()
 The current "state" of the generator.
 
uint64_t initstate () const
 The state initiator that was used upon construction.
 
template<typename R >
initstate () const
 The state initiator that was used upon construction.
 
uint64_t initseq () const
 The sequence initiator that was used upon construction.
 
template<typename R >
initseq () const
 The sequence initiator that was used upon construction.
 
template<typename T >
void restore (T state)
 Restore a given state in the sequence.
 
int64_t operator- (const pcg32 &other) const
 The distance between two PCG32 pseudorandom number generators.
 
template<typename R = int64_t>
distance (const pcg32 &other) const
 The distance between two PCG32 pseudorandom number generators.
 
template<typename R = int64_t, typename T , std::enable_if_t< std::is_integral< T >::value, bool > = true>
distance (T other_state) const
 The distance between two states.
 
template<typename T >
void advance (T distance)
 Multi-step advance function (jump-ahead, jump-back).
 
bool operator== (const pcg32 &other) const
 Equality operator.
 
bool operator!= (const pcg32 &other) const
 Inequality operator.
 
- Public Member Functions inherited from prrng::GeneratorBase< pcg32 >
double cumsum_random (size_t n)
 Result of the cumulative sum of n random numbers.
 
double cumsum_delta (size_t n, double scale=1)
 Result of the cumulative sum of n 'random' numbers, distributed according to a delta distribution,.
 
double cumsum_exponential (size_t n, double scale=1)
 Result of the cumulative sum of n random numbers, distributed according to an exponential distribution, see exponential_distribution(),.
 
double cumsum_power (size_t n, double k=1)
 Result of the cumulative sum of n random numbers, distributed according to a power distribution, see power_distribution(),.
 
double cumsum_gamma (size_t n, double k=1, double scale=1)
 Result of the cumulative sum of n random numbers, distributed according to a gamma distribution, see gamma_distribution(),.
 
double cumsum_pareto (size_t n, double k=1, double scale=1)
 Result of the cumulative sum of n random numbers, distributed according to a Pareto distribution, see pareto_distribution(),.
 
double cumsum_weibull (size_t n, double k=1, double scale=1)
 Result of the cumulative sum of n random numbers, distributed according to a weibull distribution, see weibull_distribution(),.
 
double cumsum_normal (size_t n, double mu=0, double sigma=1)
 Result of the cumulative sum of n random numbers, distributed according to a normal distribution, see normal_distribution(),.
 
void shuffle (Iterator begin, Iterator end)
 Draw uniformly distributed permutation and permute the given STL container.
 
auto decide (const P &p) -> typename detail::return_type< bool, P >::type
 Decide based on probability per value.
 
decide (const P &p)
 Decide based on probability per value.
 
void decide (const P &p, R &ret)
 Decide based on probability per value.
 
auto decide_masked (const P &p, const T &mask) -> typename detail::return_type< bool, P >::type
 Decide based on probability per value.
 
decide_masked (const P &p, const T &mask)
 Decide based on probability per value.
 
void decide_masked (const P &p, const T &mask, R &ret)
 Decide based on probability per value.
 
double random ()
 Generate a random number \( 0 \leq r \leq 1 \).
 
auto random (const S &shape) -> typename detail::return_type< double, S >::type
 Generate an nd-array of random numbers \( 0 \leq r \leq 1 \).
 
random (const S &shape)
 Generate an nd-array of random numbers \( 0 \leq r \leq 1 \).
 
auto random (const I(&shape)[L]) -> typename detail::return_type_fixed< double, L >::type
 Generate an nd-array of random numbers \( 0 \leq r \leq 1 \).
 
random (const I(&shape)[L])
 Generate an nd-array of random numbers \( 0 \leq r \leq 1 \).
 
randint (T high)
 Generate a random integer \( 0 \leq r < bound \).
 
auto randint (const S &shape, T high) -> typename detail::return_type< T, S >::type
 Generate an nd-array of random integers \( 0 \leq r \leq bound \).
 
randint (const S &shape, T high)
 Generate an nd-array of random integers \( 0 \leq r \leq bound \).
 
auto randint (const I(&shape)[L], T high) -> typename detail::return_type_fixed< T, L >::type
 Generate an nd-array of random integers \( 0 \leq r \leq bound \).
 
randint (const I(&shape)[L], T high)
 Generate an nd-array of random integers \( 0 \leq r \leq bound \).
 
auto randint (const S &shape, T low, U high) -> typename detail::return_type< T, S >::type
 Generate an nd-array of random integers \( low \leq r < high \).
 
randint (const S &shape, T low, U high)
 Generate an nd-array of random integers \( low \leq r < high \).
 
auto randint (const I(&shape)[L], T low, U high) -> typename detail::return_type_fixed< T, L >::type
 Generate an nd-array of random integers \( low \leq r < high \).
 
randint (const I(&shape)[L], T low, U high)
 Generate an nd-array of random integers \( low \leq r < high \).
 
double delta (double scale=1)
 Return a number distributed according to a delta distribution.
 
auto delta (const S &shape, double scale=1) -> typename detail::return_type< double, S >::type
 Generate an nd-array of numbers that are delta distribution.
 
delta (const S &shape, double scale=1)
 Generate an nd-array of numbers that are delta distribution.
 
auto delta (const I(&shape)[L], double scale=1) -> typename detail::return_type_fixed< double, L >::type
 Generate an nd-array of numbers that are delta distribution.
 
delta (const I(&shape)[L], double scale=1)
 Generate an nd-array of numbers that are delta distribution.
 
double exponential (double scale=1)
 Return a random number distributed according to an exponential distribution.
 
auto exponential (const S &shape, double scale=1) -> typename detail::return_type< double, S >::type
 Generate an nd-array of random numbers distributed according to an exponential distribution.
 
exponential (const S &shape, double scale=1)
 Generate an nd-array of random numbers distributed according to an exponential distribution.
 
auto exponential (const I(&shape)[L], double scale=1) -> typename detail::return_type_fixed< double, L >::type
 Generate an nd-array of random numbers distributed according to an exponential distribution.
 
exponential (const I(&shape)[L], double scale=1)
 Generate an nd-array of random numbers distributed according to an exponential distribution.
 
double power (double k=1)
 Return a random number distributed according to an power distribution.
 
auto power (const S &shape, double k=1) -> typename detail::return_type< double, S >::type
 Generate an nd-array of random numbers distributed according to an power distribution.
 
power (const S &shape, double k=1)
 Generate an nd-array of random numbers distributed according to an power distribution.
 
auto power (const I(&shape)[L], double k=1) -> typename detail::return_type_fixed< double, L >::type
 Generate an nd-array of random numbers distributed according to an power distribution.
 
power (const I(&shape)[L], double k=1)
 Generate an nd-array of random numbers distributed according to an power distribution.
 
double gamma (double k=1, double scale=1)
 Return a random number distributed according to a Gamma distribution.
 
auto gamma (const S &shape, double k=1, double scale=1) -> typename detail::return_type< double, S >::type
 Generate an nd-array of random numbers distributed according to a Gamma distribution.
 
gamma (const S &shape, double k=1, double scale=1)
 Generate an nd-array of random numbers distributed according to a Gamma distribution.
 
auto gamma (const I(&shape)[L], double k=1, double scale=1) -> typename detail::return_type_fixed< double, L >::type
 Generate an nd-array of random numbers distributed according to a Gamma distribution.
 
gamma (const I(&shape)[L], double k=1, double scale=1)
 Generate an nd-array of random numbers distributed according to a Gamma distribution.
 
double pareto (double k=1, double scale=1)
 Return a random number distributed according to a Pareto distribution.
 
auto pareto (const S &shape, double k=1, double scale=1) -> typename detail::return_type< double, S >::type
 Generate an nd-array of random numbers distributed according to a Pareto distribution.
 
pareto (const S &shape, double k=1, double scale=1)
 Generate an nd-array of random numbers distributed according to a Pareto distribution.
 
auto pareto (const I(&shape)[L], double k=1, double scale=1) -> typename detail::return_type_fixed< double, L >::type
 Generate an nd-array of random numbers distributed according to a Pareto distribution.
 
pareto (const I(&shape)[L], double k=1, double scale=1)
 Generate an nd-array of random numbers distributed according to a Pareto distribution.
 
double weibull (double k=1, double scale=1)
 Return a random number distributed according to a Weibull distribution.
 
auto weibull (const S &shape, double k=1, double scale=1) -> typename detail::return_type< double, S >::type
 Generate an nd-array of random numbers distributed according to a Weibull distribution.
 
weibull (const S &shape, double k=1, double scale=1)
 Generate an nd-array of random numbers distributed according to a Weibull distribution.
 
auto weibull (const I(&shape)[L], double k=1, double scale=1) -> typename detail::return_type_fixed< double, L >::type
 Generate an nd-array of random numbers distributed according to a Weibull distribution.
 
weibull (const I(&shape)[L], double k=1, double scale=1)
 Generate an nd-array of random numbers distributed according to a Weibull distribution.
 
double normal (double mu=0, double sigma=1)
 Return a random number distributed according to a normal distribution.
 
auto normal (const S &shape, double mu=0, double sigma=1) -> typename detail::return_type< double, S >::type
 Generate an nd-array of random numbers distributed according to a normal distribution.
 
normal (const S &shape, double mu=0, double sigma=1)
 Generate an nd-array of random numbers distributed according to a normal distribution.
 
auto normal (const I(&shape)[L], double mu=0, double sigma=1) -> typename detail::return_type_fixed< double, L >::type
 Generate an nd-array of random numbers distributed according to a normal distribution.
 
normal (const I(&shape)[L], double mu=0, double sigma=1)
 Generate an nd-array of random numbers distributed according to a normal distribution.
 
double draw (enum prrng::distribution distribution, std::vector< double > parameters=std::vector< double >{}, bool append_default=true)
 Get a random number according to some distribution.
 
draw (const S &shape, enum prrng::distribution distribution, std::vector< double > parameters=std::vector< double >{}, bool append_default=true)
 Get an nd-array of random numbers according to some distribution.
 
double cumsum (size_t n, enum prrng::distribution distribution, std::vector< double > parameters=std::vector< double >{}, bool append_default=true)
 Get the cumulative sum of n random numbers according to some distribution.
 

Protected Attributes

uint64_t m_initstate
 State initiator.
 
uint64_t m_initseq
 Sequence initiator.
 
uint64_t m_state
 RNG state. All values are possible.
 
uint64_t m_inc
 Controls which RNG sequence (stream) is selected. Must always be odd.
 

Detailed Description

Random number generate using the pcg32 algorithm.

The class generate random 32-bit random numbers (of type uint32_t). In addition, they can be converted to nd-arrays of random floating-point numbers (according) using derived methods from Generate().

The algorithm is full based on:

The PCG random number generator was developed by Melissa O'Neill
<oneill@pcg-random.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

For additional information about the PCG random number generation scheme,
including its license and other licensing options, visit

    http://www.pcg-random.org

Whereby most code is taken from the follow wrapper:

Wenzel Jakob, February 2015
https://github.com/wjakob/pcg32

Definition at line 2873 of file prrng.h.

Constructor & Destructor Documentation

◆ pcg32()

template<typename T = uint64_t, typename S = uint64_t>
prrng::pcg32::pcg32 ( initstate = 0x853c49e6748fea9bULL,
initseq = 0xda3e39cb94b95bdbULL 
)
inline

Constructor.

Parameters
initstateState initiator.
initseqSequence initiator.

Definition at line 2882 of file prrng.h.

Member Function Documentation

◆ advance()

template<typename T >
void prrng::pcg32::advance ( distance)
inline

Multi-step advance function (jump-ahead, jump-back).

Parameters
distanceDistance to jump ahead or jump back (depending on the sign). This changes that state of the generator by the appropriate number of increments.
Note
The method used here is based on Brown, "Random Number Generation with Arbitrary Stride", Transactions of the American Nuclear Society (Nov. 1994). The algorithm is very similar to fast exponentiation.
Author
Wenzel Jakob, https://github.com/wjakob/pcg32.

Definition at line 3264 of file prrng.h.

◆ distance() [1/2]

template<typename R = int64_t>
R prrng::pcg32::distance ( const pcg32 other) const
inline

The distance between two PCG32 pseudorandom number generators.

Template Parameters
RReturn-type. static_assert against down-casting, PRRNG_DEBUG against loss of signedness.
Returns
Distance.
Author
Wenzel Jakob, https://github.com/wjakob/pcg32.

Definition at line 3189 of file prrng.h.

◆ distance() [2/2]

template<typename R = int64_t, typename T , std::enable_if_t< std::is_integral< T >::value, bool > = true>
R prrng::pcg32::distance ( other_state) const
inline

The distance between two states.

Template Parameters
RReturn-type. static_assert against down-casting, PRRNG_DEBUG against loss of signedness.
Returns
Distance.
Warning
The increment of used to generate must be the same. There is no way of checking here!
Author
Wenzel Jakob, https://github.com/wjakob/pcg32.

Definition at line 3220 of file prrng.h.

◆ initseq() [1/2]

uint64_t prrng::pcg32::initseq ( ) const
inline

The sequence initiator that was used upon construction.

Returns
initiator.

Definition at line 3110 of file prrng.h.

◆ initseq() [2/2]

template<typename R >
R prrng::pcg32::initseq ( ) const
inline

The sequence initiator that was used upon construction.

Returns
initiator.
Template Parameters
Ruse a different return-type. There are some internal checks if the type is able to store the internal state of type uint64_t.

Definition at line 3122 of file prrng.h.

◆ initstate() [1/2]

uint64_t prrng::pcg32::initstate ( ) const
inline

The state initiator that was used upon construction.

Returns
initiator.

Definition at line 3078 of file prrng.h.

◆ initstate() [2/2]

template<typename R >
R prrng::pcg32::initstate ( ) const
inline

The state initiator that was used upon construction.

Returns
initiator.
Template Parameters
Ruse a different return-type. There are some internal checks if the type is able to store the internal state of type uint64_t.

Definition at line 3090 of file prrng.h.

◆ next_double()

double prrng::pcg32::next_double ( )
inline

Generate a double precision floating point value on the interval [0, 1).

Returns
Next random number in sequence.
Remarks
Since the underlying random number generator produces 32 bit output, only the first 32 mantissa bits will be filled (however, the resolution is still finer than in next_float(), which only uses 23 mantissa bits).
Author
Wenzel Jakob, https://github.com/wjakob/pcg32.

Definition at line 3008 of file prrng.h.

◆ next_float()

float prrng::pcg32::next_float ( )
inline

Generate a single precision floating point value on the interval [0, 1).

Returns
Next random number in sequence.
Author
Wenzel Jakob, https://github.com/wjakob/pcg32.

Definition at line 2985 of file prrng.h.

◆ next_positive_double()

double prrng::pcg32::next_positive_double ( )
inline

Generate a double precision floating point value on the interval (0, 1).

Returns
Next random number in sequence.

Definition at line 3024 of file prrng.h.

◆ next_uint32() [1/2]

uint32_t prrng::pcg32::next_uint32 ( )
inline

Draw new random number (uniformly distributed, 0 <= r <= max(uint32_t)).

This advances the state of the generator by one increment.

Returns
Next random number in sequence.
Note
Wrapper around operator().

Definition at line 2932 of file prrng.h.

◆ next_uint32() [2/2]

uint32_t prrng::pcg32::next_uint32 ( uint32_t  bound)
inline

Draw new random number (uniformly distributed, 0 <= r <= bound).

Parameters
boundBound on the return.
Returns
Next random number in sequence.
Author
Wenzel Jakob, https://github.com/wjakob/pcg32.

Definition at line 2945 of file prrng.h.

◆ operator!=()

bool prrng::pcg32::operator!= ( const pcg32 other) const
inline

Inequality operator.

Parameters
otherThe generator to which to compare.
Author
Wenzel Jakob, https://github.com/wjakob/pcg32.

Definition at line 3310 of file prrng.h.

◆ operator()()

uint32_t prrng::pcg32::operator() ( )
inline

Draw new random number (uniformly distributed, 0 <= r <= max(uint32_t)).

This advances the state of the generator by one increment.

Returns
Next random number in sequence.
Author
Melissa O'Neill, http://www.pcg-random.org.

Definition at line 2915 of file prrng.h.

◆ operator-()

int64_t prrng::pcg32::operator- ( const pcg32 other) const
inline

The distance between two PCG32 pseudorandom number generators.

Template Parameters
RReturn-type. static_assert against down-casting, PRRNG_DEBUG against loss of signedness.
Returns
Distance.
Author
Wenzel Jakob, https://github.com/wjakob/pcg32.

Definition at line 3153 of file prrng.h.

◆ operator==()

bool prrng::pcg32::operator== ( const pcg32 other) const
inline

Equality operator.

Parameters
otherThe generator to which to compare.
Author
Wenzel Jakob, https://github.com/wjakob/pcg32.

Definition at line 3298 of file prrng.h.

◆ restore()

template<typename T >
void prrng::pcg32::restore ( state)
inline

Restore a given state in the sequence.

See state().

Template Parameters
Ruse a different return-type. There are some internal checks if the type is able to store the internal state of type uint64_t.

Definition at line 3144 of file prrng.h.

◆ seed()

void prrng::pcg32::seed ( uint64_t  initstate = 0x853c49e6748fea9bULL,
uint64_t  initseq = 0xda3e39cb94b95bdbULL 
)
inline

Seed the generator (constructor alias).

Parameters
initstateInitial state.
initseqInitial sequence.

Definition at line 2895 of file prrng.h.

◆ state() [1/2]

template<typename R >
R prrng::pcg32::state ( )
inline

The current "state" of the generator.

If the same initseq() is used, this exact point in the sequence can be restored with restore().

Returns
State of the generator.
Template Parameters
Ruse a different return-type. There are some internal checks if the type is able to store the internal state of type uint64_t.

Definition at line 3058 of file prrng.h.

◆ state() [2/2]

uint64_t prrng::pcg32::state ( ) const
inline

The current "state" of the generator.

If the same initseq() is used, this exact point in the sequence can be restored with restore().

Returns
State of the generator.

Definition at line 3046 of file prrng.h.

Member Data Documentation

◆ m_inc

uint64_t prrng::pcg32::m_inc
protected

Controls which RNG sequence (stream) is selected. Must always be odd.

Definition at line 3319 of file prrng.h.

◆ m_initseq

uint64_t prrng::pcg32::m_initseq
protected

Sequence initiator.

Definition at line 3317 of file prrng.h.

◆ m_initstate

uint64_t prrng::pcg32::m_initstate
protected

State initiator.

Definition at line 3316 of file prrng.h.

◆ m_state

uint64_t prrng::pcg32::m_state
protected

RNG state. All values are possible.

Definition at line 3318 of file prrng.h.


The documentation for this class was generated from the following file: