# R rbinom() Function

To generate binomial random numbers, use the “rbinom()” function. It is used to generate a vector of binomial distributed random variables.

## Syntax

``rbinom(n, size, prob)``

## Parameters

1. n: It is the number of trials.
2. size: It is the number of successes.
3. prob: It is the probability of success for each trial.

## Example 1: Basic usage

Generate 10 binomial distributed random numbers:

``````set.seed(99)

binom_dist_random_nums <- rbinom(10, size = 1, prob = 0.5)

print(binom_dist_random_nums)``````

Output

`` [1] 1 0 1 1 1 1 1 0 0 0``

We get an output of 10 random numbers with 0 and 1 values.

A value of 1 suggests success, and 0 suggests failure.

In this example, we defined a probability of 0.5, which means the output has a minimum of five success outcomes. Increase the probability rate to increase the binomial distribution’s success rate.

## Example 2: Generating multiple binomial random numbers

``````set.seed(123) # Setting a seed for reproducibility

multiple_random_values <- rbinom(50, size = 10, prob = 0.5)

print(multiple_random_values)
``````

Output

`````` [1] 4 6 5 7 7 2 5 7 5 5 8 5 6 5 3 7 4 2 4 8 7 6 6 9 6 6 5 5 4 3 8 7 6 6 2 5 6 4
[39] 4 4 3 5 5 4 3 3 4 5 4 7``````

The output of rbinom() is a vector of length n, where each element is a count of successes in size trials.

## Example 3: Providing the size and prob

``````set.seed(99)

binom_dist_random_nums <- rbinom(10, size = 30, prob = 0.9)

print(binom_dist_random_nums)``````

Output

``[1] 27 29 26 22 27 24 26 28 28 29``

In this example, we generated 10 binomial random numbers with a probability of success of 0.9 and 30 trials.

## Example 4: Creating a histogram

To create a histogram, use the “hist()” function and provide the binomial random numbers.

``````set.seed(99)

binom_dist_random_nums <- rbinom(10, size = 30, prob = 0.9)

hist(binom_dist_random_nums)``````

Output

That’s it.