# Simulate a random variable

I wish to simulate the random variable according to pdf $$f(x)=xe^{-x}$$ I have to feeling that I should first simulate an exponential random variable $t$ with parameter -1 and try to use the composition method, but can't seem to figure out exactly how.

Thank you in advance.

## 2 Answers

The simplest thing to do is recognize that the given density corresponds to a Gamma random variable, namely $$X \sim \operatorname{Gamma}(2, 1),$$ and therefore is the sum of two IID exponential random variables with rate parameter $1$: $$X = X_1 + X_2$$ where $X_1, X_2 \sim \operatorname{Exponential}(1)$. So a simple way to do the simulation is to generate $2n$ simulations of an exponential distribution with rate $1$, then add them in pairs to obtain $n$ simulations from $X$.

Another way would be to note the cdf is $F(x) = \int_0^x te^{-t}dt = 1-(x+1)e^{-x}$, so generate $y$ using $U(0, 1)$, and solve $F(x) = Y$ (numerically, you can write the inverse using ProductLog function if that helps ;) to get an instance of the random variable $x$ you want.