# How To Use The rnorm() Function In R

The rnorm() function in R can create a random vector having a normal distribution. This tutorial will show you how to use it in detail.

## rnorm() Function In R

rnomr() is a built-in function that takes three argument:

rnorm(n, mean, sd)

All these arguments are fairly self-explanatory: mean and sd are the mean and standard deviation of the normal distribution. n is the number of observations you want R to randomly create for you, and it is also the only required argument.

If you omit mean and sd, R will assume you want to use the standard normal distribution, meaning mean = 0 and sd = 1. The returned value is a vector whose length equals to n. It contains all the random variates rnorm() generates.

This is a simple use of rnomr() to simulate 100 random variates that have the standard normal distribution.

rnorm(10)

 [1] -0.08526766 -0.17790068 -0.37182620 0.83794813 2.04833435 0.31162834 -0.84182887
[8] -2.03736916 0.60543529 1.26049442

The result will change every time the above command is executed, even when you don’t modify it:

rnorm(10)

 [1] 0.14758977 0.59181076 0.45482459 1.77804471 -0.09378004 -0.17383653 0.65331405
[8] -1.35005627 -0.20263717 0.37389574

To generate random variates for other normal distributions, just change the mean and sd arguments:

rnorm(10, mean = 1, sd = 3)

 [1] -4.8440920 3.1971416 -0.4190495 -1.7150984 2.0933391 0.6602045 3.8815517 -1.3296203
[9] -3.6718179 4.6255194

We can use the summary() function to check for essential statistical information of the generated data set:

summary(rnorm(1000))

    Min. 1st Qu. Median Mean 3rd Qu. Max.
-3.27615 -0.68920 -0.01486 -0.01832 0.63060 2.91676

As you can see, the mean and other parameters aren’t the same as those of a perfect distribution, but they get close enough.

R can easily generate a large number of random variates as well. The snippet below creates a million random numbers having the standard normal distribution. We then use the hist() function to create a histogram based on those values.

n <- rnorm(1000000)
x11();
hist(n, breaks = 100, col = "orange")


The histogram shows that the vector n contains numeric values that truly illustrate the well-known standard normal distribution. They center around zero, and most of them are within the interval (-3.15, 3.15).

You can also use these data to plot other graphs, such as with plot(). This is a scatterplot of 1000 random variates of the standard normal distribution.

plot(rnorm(1000))

To show the nature of this distribution, we can use the density() function before passing the result to plot(). This combination will create a kernel density plot – a better choice for visualizing the standard normal distribution.

plot(density(rnorm(10000)))

The more random numbers you can generate, the closer our plot will get to the ideal distribution. Due to the limited number of variates and their randomness, we can observe some small peaks.

## Summary

You can use the rnorm() function in R to generate random numbers that have a normal distribution. The output can then be used for other purposes like graphing.

Maybe you are interested:

Posted in R