# How to Use the rnorm() Function in R

The rnorm() function in R is “used to generate a vector of normally distributed random numbers”. It takes three arguments: n is the sample size, the mean is the mean of the normal distribution, and sd is the standard deviation.

### Syntax

``rnorm(n, mean, sd)``

### Parameters

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.

## Example 1: Simple R program to show the use of the rnorm() function

``````data <- rnorm(10)
data``````

Output

`````` -0.359721535 1.516744916 -0.380787719 0.345410241 0.321703671
-0.436644645 -0.267328311 -1.640269174 -0.190012636 -0.004461941``````

## Example 2: Control the number of observations generated in the rnorm() function

You can control the observations generated by the rnorm() function by providing the n argument.

For example, using the code below, you can generate a random sample of size 20 from a normal distribution with a mean of 0 and a standard deviation of 1.

``random_sample <- rnorm(n = 20, mean = 0, sd = 1)``

The “n” argument defines the number of random samples generated from the normal distribution in the above code. Pass the “n” argument per your requirement to generate random samples from a random distribution.

## Example 3: Specify the mean and standard deviation of the generated data

You can use the “mean” and “sd” arguments in the rnorm() function to define the normal distribution from which the random samples will be taken.

``output <- rnorm(n = 10, mean = 0, sd = 1)``

In this example, we passed mean = 0 and sd = 1.

So, the normal distribution will be generated based on these values(mean and sd).

## Example 4: Passing customized values of mean to rnorm() function

Let’s find normalized values that cumulatively have a mean of 1.9.

``````data <- rnorm(1:21, 1.9)
print(data)
summary(data)``````

Output

`````` 0.38755150 2.75291745 1.85497073 3.06377678 2.19992803 2.19544549
 3.06391987 2.11362968 0.08858567 1.80981794 0.62872918 1.95926405
 1.00410509 3.33564571 1.56683186 1.07596651 1.19985883 3.28332289
 2.07253467 0.85443979 3.74952206

Min.   1st Qu.  Median   Mean     3rd Qu.    Max.
0.08859  1.07597  1.95926  1.91718  2.75292   3.74952
``````

In this example, we checked that the mean of the formed normalized values is 1.9 using the summary() function.

The distributed values are up to 21 because our vector size is 21.

### 1 thought on “How to Use the rnorm() Function in R”

1. Pretty! This was an incredibly wonderful article.
Thanks for supplying these details.