Whether working on a machine learning project, a simulation, or other models, you need to generate random numbers in your code. R as a programming language, has several functions for random number generation. In this post, you will learn about them and see how they can be used in a larger program. Specifically, you will learn
- How to generate Gaussian random numbers into a vector
- How to generate uniform random numbers
- How to manipulate random vectors and random matrices
Let’s get started.

Generating Random Numbers in R.
Photo by Kaysha. Some rights reserved.
Overview
This post is divided into three parts; they are:
- Random Number Generators
- Correlated Multivariate Gaussian Random Number
- Generating Random Numbers From Uniform Distribution
Random Number Generators
Random numbers are drawn from probability distributions. The most famous distribution should be the Gaussian distribution, also known as the normal distribution.
The standard normal distribution is defined by its density function,
In R, you have not only the random number generator function based on the standard normal distribution but a few other auxiliary functions as well:
dnorm(x): The density functionpnorm(x): The distribution functionqnorm(x): The quantile functionas the inverse function of rnorm(k): The random number generator function
The functions dnorm(x), pnorm(x), qnorm(x) would be useful in some projects that you need to deal with some other probability distribution. The function rnorm(k) is the function that gives you a vector of k random values that are drawn from the standard normal distribution. This is the function you use most often.
You can tell that this is the random number generator that produces output from the standard normal distribution by getting a lot of samples from it and plot a histogram:

Histogram of Gaussian random numbers
You can see a bell-shaped histogram centered at zero in the plot. Also, you can tell that majority of the samples are between -1 to +1 (68% of them, to be precise). The freq=FALSE parameter in the hist() function makes the
Correlated Multivariate Gaussian Random Number
One common use case of standard normal random number generator is to create pairs of correlated Gaussian random number. In other words, you want some bivariate Gaussian random numbers with non-zero correlation.
The algorithm to generate such correlated random numbers is:
- First, generate same number of independent standard normal random numbers
- Set up the covariance matrix to specify how the different random numbers are related
- Take the Cholesky decomposition of the covariance matrix
- Multiply the matrix of independent standard normal random numbers with the Cholesky decomposition matrix
- Optionally, adjust the mean
In code, it is:
Most of the code above is intuitive. The tricky part is adjusting the mean: Standard Gaussian random numbers are centered at zero. The Cholesky decomposition matrix obtained from the line cholesky <- chol(cov) can only adjust the covariance without adjusting the mean. To shift the mean value, you need to add a value uniformly on each column. In the above, you repeat the vector into a matrix of the same shape as the random observation so you can add them.
How you can tell this is correct? One way is to verify the statistics. You can compute and print the correlation matrix, the column mean, and column standard deviation as follows:
These three lines should print the empirical correlation matrix, the mean, and the standard deviation respectively. You can also try to plot the random numbers as a scatter plot:
Which should look like the following:

Bivariate Gaussian random numbers with mean at (0,1) and correlation 0.6
Putting everything together, the following is the complete code:
Generating Random Numbers From Uniform Distribution
Gaussian is the most used distribution. But there are chances that you will need a different distribution. One example is to simulate the arrival time of the next phone call on a hotline. This is a classic example of the exponential distribution. Obviously, it is not Gaussian since Gaussian random values can be negative.
The exponential distribution has a density function
With an arbitrary distribution function, you can use the inverse transform sampling method to generate random numbers using a uniform distribution. The idea is that you can consider
The inverse function in exponential distribution is trivial: Set
In the above, you used not rnorm() function but runif() function for a uniform distribution between 0 and 1. There are a whole set of functions for uniform distribution, too, such as dunif() and punif().
You knows the generated numbers follows an exponential distribution if you plot the histogram from the generated numbers:

Exponential distribution generated empirically.
This technique would be useful when you build a discrete event simulation model in R.
Further Readings
You can learn more about the above topics from the following:
Website
- The R Manuals
- Multivariate normal distribution, Wikipedia
- Exponential Distirbution, Wikipedia
- Inverse transform sampling, Wikipedia
Summary
In this post, you learned how to generate random numbers in R. Specifically, you learned:
- The suite of functions in R for probability distributions and random number generation
- How to create random numbers in Gaussian distribution
- How to create random numbers in uniform distribution
- How to make use of the random number generators to create multivariate Gaussian distribution or other distribution using the inverse transform method.

No comments:
Post a Comment