Generating Random Numbers in R

Simulation is an important topic for both statistics and for a variety of other areas where there is a need to introduce randomness. Sometimes you want to implement a statistical procedure that requires random number generation or sampling. R comes with a set of pseuodo-random number generators that allow you to simulate from wellknown probability distributions like the Normal, Poisson, and binomial. Some example functions for probability distributions in R

• rnorm: generate random Normal variates with a given mean and standard deviation
• dnorm: evaluate the Normal probability density (with a given mean/SD) at a point (or vector of points)
• pnorm: evaluate the cumulative distribution function for a Normal distribution
rpois: generate random Poisson variates with a given rate

For each probability distribution there are typically four functions available that start with a “r”, “d”, “p”, and “q”. The “r” function is the one that actually simulates randon numbers from that distribution. The other functions are prefixed with a
• d for density
• r for random number generation
• p for cumulative distribution
• q for quantile function (inverse cumulative distribution)

If you’re only interested in simulating random numbers, then you will likely only need the “r” functions and not the others. However, if you intend to simulate from arbitrary probability distributions using something like rejection sampling, then you will need the other functions too. Probably the most common probability distribution to work with the is the Normal distribution (also
known as the Gaussian). Working with the Normal distributions requires using these four functions

dnorm(x, mean = 0, sd = 1, log = FALSE)
pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
rnorm(n, mean = 0, sd = 1)

Simulate standard Normal random numbers with mean 0 and standard deviation 1.


x <- rnorm(5)


[1] -0.2645712 -0.9123511 -0.6744032 1.1964118 0.4598051

You can modify the default parameters to simulate numbers with mean 10 and standard deviation 5.


x <- rnorm(6, 10,5)


> x
[1] 0.5288111 11.1151473 13.3158657 12.9255339 11.0753713 8.3695688
> summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.5288 9.0460 11.0953 9.5550 12.4729 13.3159

pipeline operater in dplyr

Random Number Seed