How to Generate Normal Distribution Random Numbers in R

The normal distribution is a continuous probability distribution defined by its mean and standard deviation. It is also known as the Gaussian distribution or bell curve because of its characteristic shape.

Generate Normal Distribution Random Numbers in R

To generate random numbers from a normal distribution in R, use the rnorm() function. The rnorm() function generates random numbers from a normal, continuous probability distribution with a bell-shaped curve.

Let’s generate 10 random numbers from a normal distribution using the rnorm() function.

set.seed(99)

normal_dist_random_nums <- rnorm(10)

print(normal_dist_random_nums)

Output

 [1] 0.2139625 0.4796581 0.0878287 0.4438585 -0.3628379 0.1226740
 [7] -0.8638452 0.4896243 -0.3641169 -1.2942420

In this example, we generated random numbers from a standard normal distribution.

In the above code, the mean is 0, and the standard deviation is 1.

Using a set.seed() function ensures that we get the same results for randomization.

Using mean and sd to generate random numbers

In the rnorm() function, mean and sd are optional arguments you can specify based on your requirements.

Use the following code to generate 10 random numbers from a normal distribution with a mean of 40 and a standard deviation of 20.

set.seed(99)

normal_dist_random_nums <- rnorm(900, mean = 30, sd = 60)
hist(normal_dist_random_nums)

Output

[1] 44.27925 49.59316 41.75657 48.87717 32.74324 42.45348 22.72310 49.79249
[9] 32.71766 14.11516

In this example, we are generating random numbers from normal distribution provided the mean of 40 and sd of 20.

Creating a histogram based on normal distribution

To create a histogram in R, use the hist() function and provide the generated random numbers.

set.seed(99)

normal_dist_random_nums <- rnorm(10, mean = 40, sd = 20)
hist(normal_dist_random_nums)

Output

Creating a histogram based on normal distribution

I have used RStudio to show the plot using the hist() function.

Let’s see another example.

set.seed(99)

normal_dist_random_nums <- rnorm(900, mean = 30, sd = 60)

hist(normal_dist_random_nums)

Output

histogram of random normal distrubution numbers

Conclusion

To generate a uniform distribution random numbers in R, use the runif() function.

To generate exponentially distributed random numbers in R, use the rexp() function.

To generate binomial random numbers in R, use the rbinom() function.

To generate normal distributed random numbers in R, use the rnorm() function.

That’s it.

Leave a Comment