bernoulli

group turbo_random_bernoulli

Functions

template<typename URBG>
bool bernoulli(URBG &&urbg, double p)

turbo::bernoulli(bitgen, p) turbo::bernoulli produces a random boolean value, with probability p (where 0.0 <= p <= 1.0) equaling true.

Prefer turbo::bernoulli to produce boolean values over other alternatives such as comparing an turbo::uniform() value to a specific output.

See https://en.wikipedia.org/wiki/Bernoulli_distribution

Example:

cpp
       turbo::BitGen bitgen;
       ...
       if (turbo::bernoulli(bitgen, 1.0/3721.0)) {
            std::cout << "Asteroid field navigation successful.";
       }

Template Parameters:

URBG – random generator

Parameters:
  • urbg – random generator

  • p – probability

Returns:

bool

inline bool bernoulli(double p)

turbo::bernoulli(p) is similar to turbo::bernoulli(bitgen, p) but uses a thread-local random generator for efficiency and convenience.

See also

turbo::bernoulli(bitgen, p)

See also

turbo::get_tls_bit_gen()

See also

turbo::set_tls_bit_gen()

Note

This function is thread-safe.

Parameters:

p – probability

Returns:

bool

inline bool fast_bernoulli(double p)

turbo::fast_bernoulli(p) is similar to turbo::bernoulli(bitgen, p) but uses a thread-local insecure random generator for efficiency and convenience.

in this case, the random generator is faster than turbo::bernoulli(p).

See also

turbo::bernoulli(bitgen, p)

See also

turbo::get_tls_fast_bit_gen()

See also

turbo::set_tls_fast_bit_gen()

Note

This function is thread-safe.

Parameters:

p – probability

Returns:

bool

class bernoulli_distribution
#include <bernoulli_distribution.h>

turbo::bernoulli_distribution is a drop in replacement for std::bernoulli_distribution.

It guarantees that (given a perfect UniformRandomBitGenerator) the acceptance probability is exactly equal to the given double.

The implementation assumes that double is IEEE754 Example:

#include <turbo/random/bernoulli_distribution.h>
#include <iostream>
#include <turbo/random/random.h>

int main() {
    turbo::BitGen gen;
    turbo::bernoulli_distribution dist(0.5);
    std::cout << dist(gen) << std::endl;
    return 0;
}

Public Functions

inline bernoulli_distribution()

Constructs a new bernoulli_distribution object.

inline explicit bernoulli_distribution(double p)

Constructs a new bernoulli_distribution object.

Parameters:

p – The probability of success

inline void reset()

Resets the distribution state.

template<typename URBG>
inline bool operator()(URBG &g)

Generates a random number.

Template Parameters:

URBG – The type of the random number generator

Parameters:

g – The random number generator

Returns:

result_type The generated random number

template<typename URBG>
inline bool operator()(URBG &g, const param_type &param)

Generates a random number.

Template Parameters:

URBG – The type of the random number generator

Parameters:
  • g – The random number generator

  • param – The distribution parameters

Returns:

result_type The generated random number

inline param_type param() const

Returns the distribution parameters.

Returns:

param_type The distribution parameters

inline void param(const param_type &param)

Sets the distribution parameters.

Parameters:

param – The distribution parameters

inline double p() const

Returns the minimum value that can be generated.

Returns:

result_type The minimum value that can be generated

class param_type
#include <bernoulli_distribution.h>

The type of the distribution parameters.

Public Functions

inline explicit param_type(double p = 0.5)

Constructs a new param_type object.

Parameters:

p – The probability of success

inline double p() const

Returns the probability of success.

Returns:

double The probability of success

Friends

inline friend bool operator==(const param_type &p1, const param_type &p2)

operator== for param_type objects Returns true if the two param_type objects are equal Returns false if the two param_type objects are not equal

inline friend bool operator!=(const param_type &p1, const param_type &p2)

operator!= for param_type objects Returns true if the two param_type objects are not equal Returns false if the two param_type objects are equal