This article about R’s rnorm function is part of a series we’re doing about generating random numbers using the R language. Our earlier sets of examples dealt with randomly picking from a list of discrete values and the uniform distributions. The R rnorm function offers similar functionality for the normal distribution, which is a commonly requested for scientific and business analysis.
R’s rnorm and the Standard Normal Distribution
We’re going to start by introducing the rnorm function and then discuss how to use it.
R’s rnorm function takes the parameters of a normal distribution and returns X values as a list. The expected syntax is:
rnorm (n, mean = x, sd = y)
- n – number of observations we want rnorm to return
- mean – mean value of the normal distribution we are using
- sd – standard deviation of the normal distribution we are using
If we wanted to generate value from a standard normal distribution, where mean = 0 and the standard deviation is 1, we would code it as:
rnorm(5, mean=0, sd=1)  0.46704102 -0.36129104 -0.07062314 1.40160030 0.16795590
As we can see, this function generates an appropriate looking set of values.
An example of a regular normal distribution:
rnorm(5, mean=20, sd=5)  27.35130 15.00245 16.76702 23.17056 31.29196
Again, using rnorm to generate a set of values from the distribution.
Using rnorm & The Normal Distribution
The normal distribution is broadly used in the sciences and business. It represents the convergence of the average of a set of samples from a uniform distribution. This is the traditional “bell curve”.
This distribution works in the real world due to the nature of how most processes operate. Most results are affected by several process steps. A widget might cut by saw A. Then saw B. After which, we cut and wrap a set of 20 widgets into a bundle. Perhaps a few widgets may be bumped at different points on the conveyor belt. The final width of a widget is the sum of these little errors.
If we assume each of the “little errors” is uniformly distributed, the sum of these errors will converge on the normal distribution.
So you can use the normal distribution for a wide range of things:
- Stock Market Returns
- Athletic Performance
- Test Scores
- Sports Scores
Thus, rnorm can be a pretty handy function for simulation modeling and testing.
Related functions: rnorm, pnorm, qnorm, dnorm
Want to validate the random values you generated?
That’s easy – check the random number sample against the probability distribution function. In this case, you’re comparing the random variable against the standard distribution. You can look at specific intervals or the cumulative density of the normal model.
Take a look at the R’s pnorm function, which returns the cumulative pdf. This is a digital version of the table of probabilities included as an appendix in your favorite statistics book.
Need to set a cutoff score for a given point in the normal distribution?
Take a look at R’s qnorm function, which is the inverse of pnorm (the cdf). This will generate the z-score associated with the n’th quantile of the normal distribution.
Need a standard probability density function for the normal distribution?
You should use R’s dnorm function.
Taken as a group, you can use these functions to generate the normal distribution in R.
Need something more basic? Use the quantile function to inspect intervals. You can calculate the sample mean based on the R function here. Plot a histogram and compare it to the normal curve for a normal random variable with a given mean. Your sample function should generate values that fit within these patterns.
This article on rnorm in R is part of our series on sampling in R. To hop ahead, select one of the following links: