View source: R/distributions.r
rbernoulli | R Documentation |
A very fast implementation for generating bernoulli trials. Can take a vector of probabilities which makes it very useful for simulation studies.
rbernoulli(n, p=0.5, output="logical", reference=NULL)
n |
How many draws to make. |
p |
A numeric vector of probabilities, used when drawing the trials. |
output |
A single character string, specifying which format the output should be returned as. Must be one of |
reference |
A single character string, specifying which of the two possible values should be considered the reference when |
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
.
Returns a vector of length n
in the desired output format.
Robin Denz
library(simDAG)
# generating 5 bernoulli random draws from an unbiased coin
rbernoulli(n=5, p=0.5)
# using different probabilities for each coin throw
rbernoulli(n=5, p=c(0.1, 0.2, 0.3, 0.2, 0.7))
# return as numeric instead
rbernoulli(n=5, p=0.5, output="numeric")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.