# Generate Random Values with Fixed Mean & Standard Deviation in R (2 Examples)

In this tutorial youâ€™ll learn how to **sample random numbers with fixed mean and standard deviation** in R programming.

The article contains two examples for the sampling of random numbers with a fixed mean and standard deviation. To be more specific, the article will consist of the following content blocks:

Letâ€™s take a look at some R codes in action!

## Example 1: Sample Random Values without Fixed Mean & Standard Deviation Using rnorm() Function

In Example 1, Iâ€™ll illustrate how a random number generating process would usually be applied, i.e. without fixed mean and standard deviation.

Letâ€™s first set a random seed to guarantee the reproducibility of the following examples:

set.seed(5793645) # Set random seed for reproducibility

In the next step, we can draw random values from the normal distribution using the rnorm function. Note that we are specifying a mean of 5 and a standard deviation of 2 in the following syntax:

x1 <- rnorm(100, 5, 2) # Apply rnorm function head(x1) # First six random values # [1] 1.953203 4.430239 7.692162 7.503771 3.428893 2.253762

The previous output shows the first six out of 100 randomly generated numbers.

Letâ€™s use the mean function to calculate the average of this vector of random values:

mean(x1) # Mean of random values # [1] 4.799771

As you can see, the mean is close to the specified mean (i.e. 5), but itâ€™s not exactly that mean.

Similar to that, we can print the standard deviation of our random values using the sd function:

sd(x1) # Standard deviation of random values # [1] 1.921638

Again, the result is close to the specified standard deviation (i.e. 2), but itâ€™s not exactly the result we wanted.

The reason for that is the randomness of of the previous processes. Due to this randomness, the mean and standard deviation are slightly different from our specifications.

You may increase the sample size to get closer to the desired values, but you will probably never get the exact mean and standard deviation that you have specified.

So how can we do that? Correct, thatâ€™s what Iâ€™ll explain next!

## Example 2: Sample Random Values with Fixed Mean & Standard Deviation Using rnorm() & scale() Functions

In Example 2, Iâ€™ll explain how to generate random numbers with fixed mean and standard deviation values.

For this task, we first have to create a user-defined function that modifies our random values.

Within this user-defined function, we are applying the scale function to our random values to set a fixed standard deviation.

Furthermore, we are using the as.vector function to convert the output of the scale function to a vector object.

Consider the R code below:

rnorm_fixed <- function(n, mean, sd) { # Create user-defined function as.vector(mean + sd * scale(rnorm(n))) }

Next, we can apply our user-defined function to create a vector object with a certain sample size, a fixed mean, and a fixed standard deviation:

x2 <- rnorm_fixed(100, 5, 2) # Apply user-defined function head(x2) # First six random values # [1] 7.375810 4.944177 7.280842 5.155544 6.067030 5.257782

Letâ€™s check the meanâ€¦

mean(x2) # Mean of random values # [1] 5

â€¦and the standard deviation of this randomly generated vector:

sd(x2) # Standard deviation of random values # [1] 2

The mean is 5 and the standard deviation is 2 â€“ Exactly as we have specified!

## Video & Further Resources

Some time ago, I have published a video on my YouTube channel, which illustrates the R syntax of this article. Please find the video below.

Besides the video, you may want to have a look at some of the other articles on this homepage. I have released numerous posts already.

- Replace Missing Values by Column Mean in R
- Generate Set of Random Integers from Interval
- Replace NA Values by Row Mean in R
- Plot Mean & Standard Deviation by Group
- R Programming Tutorials

In summary: This tutorial has illustrated how to **generate random numbers with fixed mean and standard deviation** in the R programming language. In case you have additional questions or comments, please let me know in the comments section below.

## 2 Comments. Leave new

how do you apply this function to a data frame?

Hi Sara,

Could you explain your question in some more detail? I’m afraid I don’t understand what you would like to do.

Regards,

Joachim