Ask Your Question

Revision history [back]


The answer is probabilistic and may be found in most probability or statistics book. You may also have a look at wikipedia

If m is any probability measure on [a,b], then you may associate its repartition function F(t) = m([a,t]) (which is monotone non-decreasing, takes value in [0,1] and is right-continuous). You may defined an inverse for F given by G(t) = inf {x; F(x) >= t}. Now, given a uniform distributed variable U, F^{-1}(U) is distributed as m on [a,b].

In principle, this may be used to simulate any probability measure. In practice, given a density function, you need to compute the inverse of its primitive with value 0 at a (and hence value 1 at b).

As an example with f(t) = 2*t on [0,1], you obtain a random number generator with

def random_generator():
    return sqrt(random())

(because random() is uniformly distributed on [0,1] and sqrt is the inverse of t^2 = primitive(f) on [0,1]).