Get startedGet started for free

Generating random numbers

1. Generating random numbers

An important idea in statistics is simulation. A simple example is to write code to generate the results of a thousand random dice rolls. More complex examples include techniques like Monte Carlo simulations, and simulations of stochastic processes. Any kind of simulation involves the generation of random numbers - in this video you'll learn how to get started.

2. Uniform random numbers

Here's a histogram of 1000 numbers randomly generated from a Uniform distribution between zero and one. In this case, uniform doesn't refer to smart clothing; rather it means that any number in the range is equally likely to be generated. In the plot, the buckets have slightly different heights due to randomness. So, how did I make this plot?

3. Generating Uniform random numbers (1)

To generate a random number between zero and one, you simply call the RAND() function without any arguments. To generate lots of random numbers, you call RAND() lots of times. Simple!

4. Generating Uniform random numbers (2)

A related function is RANDBETWEEN(). This generates a random number between lower and upper limits that you specify. The main difference is that RANDBETWEEN() always returns whole numbers. That is, there is no fractional part.

5. Normal random numbers

Here's a histogram is 1000 normally distributed random numbers. You can see a rough bell-shaped curve, with some variation because it was randomly generated.

6. Generating normal random numbers

To generate random numbers from a normal distribution - sometimes called a Gaussian distribution - you need a trick. You pass RAND() as the first argument to the normal inverse cumulative distribution function, NORMINV(). Inverse cumulative distribution functions are beyond the scope of this course, so for now you just need to know that you pass RAND() to NORMINV(). The second and third arguments to NORMINV() are the mean and standard deviation of the normal distribution.

7. Generating numbers from other distributions

The same trick works with other distributions. Just pass RAND() as the first argument to the appropriate inverse cumulative distribution function, then subsequent arguments are the parameters for that distribution. Here you can see examples of the log-normal, T, Fisher, F, and Beta distributions.

8. Summary

To summarize, RAND() generates Uniform random numbers between zero and one. RANDBETWEEN() does the same, letting you specify the lower and upper bounds, and only returning integers. Passing RAND() to NORMINV() lets you generate normally distributed random numbers. You can also use this trick with other inverse cumulative distribution functions to generate random numbers from other distributions.

9. Let's get random!

Time to get random!