# Using a Random number Generator to draw samples from a Cumulative Distribution function

I am given a Rayleigh, distribution function:$$f(x)=\frac{1}{5}x\exp\left(\frac{-x^2}{10}\right)$$ with $x>0$ and asked to:

Use an appropriate random number generator algorithm to draw 500 samples from F(x).

What I thought on doing is using the Aceptance-rejection method :

1. Generate a rv $Y$ distributed as $G$.

2. Generate $U$ (independent from $Y$ ).

3. If $U \leq \frac{f(Y)}{cg(Y)}$ , then set $X$ $=$ $Y$ (“accept”) ; otherwise go back to 1 (“reject”).

I thought that I will use the $\chi^2$ distribution as my $g(Y)$ with $k=1$ degrees of freedom. I made that decision on the fact that both functions have the same domain, namely $x\in(0,\infty)$ and their CDFs look "similar". Therefore : $$g(x)=\frac{x^{-\frac{1}{2}}\cdot\exp\left(-\frac{x}{2}\right)}{\sqrt{2}\Gamma\left(\frac{1}{2}\right)}$$ Then $$\frac{f(x)}{g(x)}=\frac{\sqrt{2}\Gamma\left(\frac{1}{2}\right)}{5}\cdot x ^{\frac{3}{2}}\cdot\exp\left(\frac{x}{2}-\frac{x^2}{10}\right)$$ And I found out that this function has a maximum at $$x=\frac{5+\sqrt{145}}{4}$$ which is around $4.26$. Hence $$\frac{f(x)}{g(x)}\leq c=4.26$$. But I also read that $c$ value has to be as "close" to 1 as possible and I think 4.26 is not "close" Is my calculation correct? Is it entirely wrong? Should I use different method for drawing that random samples? Thanks for any method

• For your density function $f(x)$, the cumulative distribution function is $F(x)=1-e^{-\frac{\text{x}^2}{10}}$. Just generate a uniform random number $u$ on $[0,1]$ and solve for $x$ in $u=F(x)$.
– JimB
Jan 24 '18 at 18:31
• With respect to your final comment / question: $c$ tells you how many uniform variates you'll have to generate on average in order to generate one "final" variate. It's not that a larger $c$ means your algorithm doesn't work, it just means that it will run more slowly, so you'd like $c$ smaller rather than larger. But for this distribution, @JimB 's suggestion is the way to go. Jan 24 '18 at 18:52
• – Tim
Feb 3 '18 at 8:48

The simplest way is to use the cumulative distribution function like in the title of your question. As pointed by Jim B., the CDF is:

$$F(x)=1-e^{-\frac{x^2}{10}}$$

The method is explained here: wikipedia or here: How does the inverse transform method work?

The Aceptance-rejection method is more complex, usually slower, and should not be the first choice. It's only useful when the CDF has no simple closed form.

If you really want to do accept-reject I'd suggest that a $\chi^2_4$ density would be a considerably better choice as a proposal than a $\chi^2_1$. A $c$ of just under 1.5 will do (I'd just use 1.5, the difference is miniscule).

[With care, a well chosen gamma density would let you push it some way below 1.5. Probably not worth the effort though; you may be able to do considerably better with a mixture.]

However (as was suggested in comments), the inverse-cdf method is probably more convenient for the Rayleigh.