Search code examples
rsimulationbinomial-coefficients

How can I randomly sample binomial thing?


For example, I want to randomly line the 0, 1 (50% respectively) 10 times. So, there should be five "0" and five "1".

But, when I used:

rbinom(10,1,0.5)

sometimes, it generates four "0" and six "1".

I noticed that the sample() function has also this issue.

There should be five "0" and five "1", and the order should be at random.


Solution

  • sample will shuffle a vector randomly. So sample(rep(c(0,1),5)) is what you need.