How to generate Bernoulli random variable in R?

R ProgrammingServer Side ProgrammingProgramming

Each value in Bernoulli random variable represents success or a failure for a single trial that makes it different from Binomial random variable because a Binomial random variable represents number of success or failure for a number of trials. To generate a Bernoulli random variable, we can use rbinom function but we need to pass 1 for size argument.

Example

 Live Demo

rbinom(120,1,0.71)

Output

[1]   1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 0 1
[38]  1 0 1 1 1 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0
[75]  1 1 0 1 1 1 1 1 0 0 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 0 1 1 1 1 1 1 0
[112] 0 0 1 1 0 1 1 1 1

Example

 Live Demo

rbinom(120,1,0.1)

Output

[1]   0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 1 0
[38]  0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[75]  0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[112] 0 0 0 0 0 0 0 0 0

Example

 Live Demo

rbinom(120,1,0.91)

Output

[1]   1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[38]  1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[75]  1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1
[112] 1 1 1 1 1 1 1 1 1

Example

 Live Demo

rbinom(120,1,0.999)

Output

[1]   1 1 1 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