In general, you want to have the inverse cumulative probability density function. Once you do this, generating random numbers by distribution is simple:
import random def sample(n): return [ icdf(random.random()) for _ in range(n) ]
Or if you use NumPy:
import numpy as np def sample(n): return icdf(np.random.random(n))
In both cases, icdf is a function of the inverse cumulative distribution, which takes a value from 0 to 1 and outputs the corresponding value from the distribution.
To illustrate the nature of icdf , we take a simple uniform distribution between the values 10 and 12 as an example:
The probability distribution function is 0.5 between 10 and 12, zero elsewhere
the cumulative distribution function is 0 below 10 (without samples below 10), 1 above 12 (without samples above 12) and grows linearly between values (PDF integral)
the inverse cumulative distribution function is determined only between 0 and 1. At 0 it is 10, at 12 it is 1 and it varies linearly between the values
Of course, the hard part is getting the inverse cumulative density function. It really depends on your distribution, sometimes you can have an analytic function, sometimes you can resort to interpolation. Numerical methods can be useful since numerical integration can be used to create a CDF, and interpolation can be used to invert it.
source share