# How should I generate this random variable?

Suppose we now have a Erlang distribution $b(x;2,1) = x e^{-x}$. According to the definition of Erlang distribution we know that the variance of such a distribution is 2 and we want to reduce the variance of this model. So we hope that the modified time $\tilde{b}$ would fall faster than the original $b$. We're trying to use:

$\tilde{b} = \begin{cases} \frac{1}{c} x e^{-x} & 0 < x < t \\ \frac{1}{c} t e^{-x} & x \geq t\end{cases}$.

Integration tells us $c = 1 - e^{-t}$. And after we choose $c$, how should we generate such a random variable?

-
In this case, finding the inverse of the cdf is problematic, so I would look for ideas under rejection sampling. – André Nicolas Mar 1 '12 at 1:12
@AndréNicolas Thanks Andre. Can you amplify your ideas a bit? – Mathematics Lover Mar 1 '12 at 1:14
Not really, there are many "tricks of the trade," so specific suggestions I make would likely be inefficient. You can start with this link. It will provide you with the basics. Essentially you produce a pleasant larger region than the one you want, produce a random number uniformly distributed over the larger region, test whether it lies in your desired region (easy), keep if it does, try again if it doesn't. The trick is not to waste too high a proportion of your random number production. – André Nicolas Mar 1 '12 at 1:25

In order to have an answer, here is something that will work. It is based on the idea of Rejection Sampling. Call your modified density function $f(x)$.
Use a pseudo-random number generator that simulates the values of a random variable uniformly distributed on $(0,1)$. Suppose that this generator produces the numbers $u$ and $v$.
Let $x=-\ln u$. If $$f(x) \ge vte^{-x},$$ accept $x$ as coming from sampling from our distribution. Else reject $x$. Repeat. This is a close relative of the usual Monte Carlo method for finding the area of a complicated figure.