Bernoulli distribution. Returns true
with probability p
and false
with
probability 1 - p
.
Example
use std::random::{thread_rng, Bernoulli};
let dist = Bernoulli::new(0.5);
if dist.sample(thread_rng()) {
println!("Heads");
} else {
println!("Tails");
}
Run this example
Methods
impl Bernoulli { ... }
-
Creates a new Bernoulli distribution with the given probability.
Panics if
p
is not in the range[0, 1]
. -
Samples a distribution using a provided random number generator.
Mixins
impl Bernoulli { ... }
-
mixin<R> Distribution<Bernoulli, bool, R>
R: Rng<R> -
fn sample_iter(self: &Self, rng: &mut R) -> DistIterator<Self, T, R>
Raturns an iterator producing an infinite stream of values sampled from the distribution.
Example
use std::random::{thread_rng, UniformInteger}; let dist = UniformInteger::new(0..=5); // Prints 10 random numbers between 0 and 5 inclusive for i in dist.sample_iter(thread_rng()).take(10) { println!("{}", i); }
Run this example