How to Generate Random Variables from Popular Distributions in R

05.04.2021

R provides many features for generating random numbers, especially compared to othe r languages. Built in to R, you can generate random numbers from many popular distributions which is helpful when simulating data. In this article, we will learn how to generate random variables from popular distributions in R.

Many of the methods to generate random variables are prefixed with a r for random. For example, if we want to generate a random varaible form the normal distribution, we can use rnorm, for random normal.

Each method will use default paramters unless you specify them. Here are many examples.

runif(1, min = -3, max = 3)      # One uniform variate between -3 and +3
#> [1] 2.49
rnorm(1)                         # One standard Normal variate
#> [1] 1.53
rnorm(1, mean = 100, sd = 15)    # One Normal variate, mean 100 and SD 15
#> [1] 114
rbinom(1, size = 10, prob = 0.5) # One binomial variate
#> [1] 5
rpois(1, lambda = 10)            # One Poisson variate
#> [1] 12
rexp(1, rate = 0.1)              # One exponential variate
#> [1] 3.14
rgamma(1, shape = 2, rate = 0.1) # One gamma variate
#> [1] 22.3