RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Other random results
Back to Random real numbers. Forward to Other random distributions. Up to Contents.

The following routines return true with the given probability

Definitions of routines returning booleans
routine probability
Boolean() 1/2
Prob<NumericType>(NumericType p) p
Prob<NumericType>(NumericType m, NumericType n) m/n

Prob accepts both real and integer types. However, typically the one-argument version (the Bernoulli distribution) would be used with a real argument and the two-argument version would be used with integer arguments. Prob<NumericType>(NumericType p) is false if p ≤ 0 and true if p ≥ 1. Prob<NumericType>(NumericType m, NumericType n) returns false if m ≤ 0 or n < 0 and true if mn. The results of Prob are exact. In particular, Prob<double>(x, y) returns true with probability x/y even if x/y cannot be exactly represented as a double (e.g., it underflows to zero).

Equivalences between routines returning booleans
Function Equivalent
Boolean() Integer<bool>()
Boolean() Prob(1, 2)
Boolean() Prob(0.5)
Prob<RealType>(x) Float<RealType>() < x
Prob<RealType>(x) FloatU<RealType>() ≤ x

Finally, Bits<n>() returns n random bits in a std::bitset<n>. For example

RandomLib::Random r; r.Reseed();
std::cout << "Generate 20 random bits: " << r.Bits<20>() << "\n";

might produce

  Generate 20 random bits: 10110101000101001001
Back to Random real numbers. Forward to Other random distributions. Up to Contents.