Matlab: Generate Random Numbers With A Given Distribution

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

http://matlabtricks.

com/post-44/generate-random-numbers-with-a-given-distribution

matlabtricks.com
0 5 O c t o b e r, 2 0 1 4

Generate random numbers with a given distribution


The rand function in MATLAB returns uniformly distributed pseudorandom values from the open interval (0, 1), but we
often need random numbers of other kind of distributions.

A great article written by John S. Denker explains a method of generating random numbers with arbitrary distribution.
This post is based on his work, and shows a simple MATLAB implementation.

Introduction to probability functions


Suppose, that we want to pick an x random value from the (1, 11) interval and all possible values are equally probable.
The so called probability density function (PDF) of this case can be seen on the following picture:

0.12
0.1
0.08
p(x)

0.06
0.04
0.02
0
0 2 4 6 8 10 12

We have an R range and we can pick any x random value between Rmin = 1 and Rmax = 11. The probability of all
possible x values equals to 0.1.

This is the so-called uniform distribution.

By having a closer look at the p(x) function, we realize, that the area under it equals to 1. That means that if we pick a
random x value from the range (1, 11), the probability, that the value falls between 1 and 11 is exactly 1. Similarly, the
area under p(x) for a sub-range of R equals to the probability of x falling into that sub-range.

For example the probability of x falling into the (1, 6) sub-range equals to 0.1 * (6 - 1) = 0.5. Because the (1, 6)
sub-range covers the half of the possible outcomes, that is true.

Now take the integral of p(x) and have a look at the resulting P(x) function:

1 of 5 29/06/17 16:45
http://matlabtricks.com/post-44/generate-random-numbers-with-a-given-distribution

Atom Powered by Nibbleblog Zoltn Fegyver 2013

2 of 5 29/06/17 16:45
http://matlabtricks.com/post-44/generate-random-numbers-with-a-given-distribution

3 of 5 29/06/17 16:45
http://matlabtricks.com/post-44/generate-random-numbers-with-a-given-distribution

4 of 5 29/06/17 16:45
http://matlabtricks.com/post-44/generate-random-numbers-with-a-given-distribution

5 of 5 29/06/17 16:45

You might also like