Binomial Random Numbers Generation in R

We will learn how to generate Bernoulli or Binomial Random Numbers (Binomial distribution) in R with the example of a flip of a coin. This tutorial is based on how to generate random numbers according to different statistical probability distributions in R. Our focus is on binomial random numbers generation in R.

Binomial Random Numbers in R

We know that in Bernoulli distribution, either something will happen or not such as a coin flip has two outcomes head or tail (either head will occur or head will not occur i.e. tail will occur). For an unbiased coin, there will be a 50% chance that the head or tail will occur in the long run. To generate a random number that is binomial in R, use the rbinom(n, size, prob) command.

rbinom(n, size, prob) #command has three parameters, namey

where
‘$n$’ is the number of observations
‘$size$’ is the number of trials (it may be zero or more)
‘$prob$’ is the probability of success on each trial for example 1/2

Examples of Generation Binomial Random Numbers

  • One coin is tossed 10 times with a probability of success=0.5
    the coin will be fair (unbiased coin as p=1/2)
    rbinom(n=10, size=1, prob=1/2)
    OUTPUT: 1 1 0 0 1 1 1 1 0 1
  • Two coins are tossed 10 times with a probability of success=0.5
  • rbinom(n=10, size=2, prob=1/2)
    OUTPUT: 2 1 2 1 2 0 1 0 0 1
  • One coin is tossed one hundred thousand times with a probability of success=0.5
    rbinom(n=100,000, size=1, prob=1/2)
  • store simulation results in $x$ vector
    x <- rbinom(n=100000, size=5, prob=1/2)
    count 1’s in x vector
    sum(x)
    find the frequency distribution
    table(x)
    creates a frequency distribution table with frequency
    t = (table(x)/n *100)
    plot frequency distribution table
    plot(table(x),ylab = "Probability",main = "size=5,prob=0.5")
Binomial Random Numbers

View the Video tutorial on rbinom command

Learn Basic Statistics and Online MCQs about Statistics

Leave a Reply

Discover more from R Language Frequently Asked Questions

Subscribe now to keep reading and get access to the full archive.

Continue reading