The rnorm() function generates a random number using a normal(bell curve) distribution. The rnorm() function simulates random variates having a specified normal distribution.
What is Normal Distribution in R
The normal distribution is the collection of random data from independent sources is distributed normally. We get the bell shape curve by plotting a graph with the variable’s value on the horizontal axis and the values’ count on the vertical axis. The center of the curve represents the mean of the dataset.
Functions in R for Probability Distributions
R distribution handles four functions to generate random numbers. There is a root name. The root name for the normal distribution is the norm. This root is prefixed by one of the following letters.
- The p is for “probability” and it is a cumulative distribution function. For example, pnorm() function.
- The q is for “quantile“, and it is an inverse. For example, qnorm() function.
- The d is for “density“, and it is a density function. For example, dnorm() function.
- The r is for “random“, and it is a random variable having the specified distribution. For example, rnorm() function.
We will talk about rnorm() in this tutorial. As with pnorm(), qnorm(), and dnorm(), the optional arguments define the mean and standard deviation of the distribution.
rnorm() Function in R
The rnorm() is a built-in R function that generates a vector of normally distributed random numbers. It generates the Normal Distributions On Special Spaces.
The rnorm() function helps to generate random numbers whose distribution is normal. The rnorm() method takes a sample size as input and generates that many random numbers.
rnorm(n, mean, sd)
n: It is the number of observations(sample size).
mean: It is the mean value of the sample data. Its default value is zero.
sd: It is the standard deviation. Its default value is 1.
data <- rnorm(10) data
 -0.359721535 1.516744916 -0.380787719 0.345410241 0.321703671 -0.436644645 -0.267328311  -1.640269174 -0.190012636 -0.004461941
Create a histogram based on rnorm()
To create a histogram in R, use the hist() function. A histogram depicts the frequencies of values of a variable bucketed into ranges. The histogram is similar to the bar chart, but it groups the values into continuous ranges.
To create a normal distribution in R, use the rnorm() function. Let’s generate 30 random numbers using the rnorm() function and create a histogram based on that distribution.
data <- rnorm(30) hist(data, main="Normal distribution")
And we get the bar chart type of histogram based on the random data.
Generate three different vectors of random numbers in R
Let’s generate three different vectors of random numbers in R using the rnorm() function.
Let’s generate histograms based on these random numbers.
k10 <- rnorm(10, mean = 50, sd = 8) k100 <- rnorm(100, mean = 50, sd = 8) k1000 <- rnorm(10000, mean = 50, sd = 8) oldpar <- par() par(mfrow=c(1,3)) # The breaks argument specifies how many bars are in the histogram hist(k10, breaks = 5) hist(k100, breaks = 20) hist(k1000, breaks = 80)
You can see that we have created three histograms using three different normal distributions.
To generate a vector of normally distributed random numbers in R, use the rnorm() function. The first argument n is the number of numbers you want to generate, followed by the standard mean and sd arguments.