Generate Random Draws from a Bernoulli Distribution
rbernoulli.Rd
A very fast implementation for generating bernoulli trials. Can take a vector of probabilities which makes it very useful for simulation studies.
Details
Internally, it uses only a single call to runif
, making it much faster and more memory efficient than using rbinomial
.
Note that this function accepts values of p
that are smaller then 0 and greater than 1. For p < 0
it will always return FALSE
, for p > 1
it will always return TRUE
.
Examples
library(simDAG)
# generating 5 bernoulli random draws from an unbiased coin
rbernoulli(n=5, p=0.5)
#> [1] TRUE TRUE FALSE FALSE TRUE
# using different probabilities for each coin throw
rbernoulli(n=5, p=c(0.1, 0.2, 0.3, 0.2, 0.7))
#> [1] FALSE FALSE FALSE FALSE TRUE
# return as numeric instead
rbernoulli(n=5, p=0.5, output="numeric")
#> [1] 0 0 1 0 0