choose number with specific probability using uniform distribution

37 Views Asked by At

Say we must choose between {0, 1} and the probability p stands for choosing 1 for multiple times (e.g.10 times) So when p=0.8 we choose 1 with 80% and 0 with 20%. The following R code will do the work. Could someone explain the theoretical background behind it? It looks very common.

  ifelse(runif(10)<0.8,1,0)