Generating binomial random variates

To model the success or failure of several independent trials, one can generate samples from binomial distribution. In this recipe, we will discuss how to generate binomial random variates with R.

Getting ready

In this recipe, you need to prepare your environment with R installed.

How to do it…

Please perform the following steps to create a binomial distribution:

  1. First, we can use rbinom to determine the frequency of drawing a six by rolling a dice 10 times:
    > set.seed(123)
    > rbinom(1, 10, 1/6)
    [1] 1
    
  2. Next, we can simulate 100 gamblers rolling a dice 10 times, and observe how many times a six is drawn by each gambler:
    > set.seed(123)
    > sim <- rbinom(100,10,1/6) 
    > table(sim)
    sim
     0  1  2  3  4  5 
    17 36 23 18  4  2 
    
  3. Additionally, ...

Get R for Data Science Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.