isabel_espana
isabel_espana

Reputation: 11

Generate binomial samples determining the probability

I want to generate n samples of binary values (0 or 1) randomly:

This is easy with runif:

samples <- round(runif(n,min=0, max=5))

But the probability of having 0 or 1 is 50%. But, what if I want to state a probability of for example 30% of having 1 and 70% of having 0?

I thank you a lot

Upvotes: 0

Views: 256

Answers (1)

Roland
Roland

Reputation: 132969

If you want to sample from the binomial distribution, use rbinom:

set.seed(42) #for reproducibility
#the sample:
res <- rbinom(n = 1000, size = 1, prob = 0.3)

#check probabilities
mean(res)
#[1] 0.293

Upvotes: 1

Related Questions