RandomLib
1.10
|
The following routines return true with the given probability
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 m ≥ n. 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).
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
might produce
Generate 20 random bits: 10110101000101001001