# Using random number generator to draw from population

Suppose X is exponentially distributed, f(x) = e^(x/10)/10. How would you use a random number generator to generate a sample of observations from this population?

-
$\displaystyle \int_{x = -\infty}^\infty f(x) dx \ne 1$ in your case. – Kaster Feb 27 '14 at 23:26
I assume he means the exponential distribution from $0$ to $\infty$ – Jimmy R. Feb 27 '14 at 23:27

The method you are looking for is called Inverse transform sampling and is explained here

-

Draw a random number from a uniform. Then determine the inverse of the distribution function and evaluate it at your random number. The resulting number is a random number drawn from your desired distribution.

The distribution function is the integral of the density function that you provide. This works for any distribution albeit that the inverse distribution function is often not as easy to compute as it is here.

-

$\newcommand{\+}{^{\dagger}}% \newcommand{\angles}[1]{\left\langle #1 \right\rangle}% \newcommand{\braces}[1]{\left\lbrace #1 \right\rbrace}% \newcommand{\bracks}[1]{\left\lbrack #1 \right\rbrack}% \newcommand{\ceil}[1]{\,\left\lceil #1 \right\rceil\,}% \newcommand{\dd}{{\rm d}}% \newcommand{\down}{\downarrow}% \newcommand{\ds}[1]{\displaystyle{#1}}% \newcommand{\equalby}[1]{{#1 \atop {= \atop \vphantom{\huge A}}}}% \newcommand{\expo}[1]{\,{\rm e}^{#1}\,}% \newcommand{\fermi}{\,{\rm f}}% \newcommand{\floor}[1]{\,\left\lfloor #1 \right\rfloor\,}% \newcommand{\half}{{1 \over 2}}% \newcommand{\ic}{{\rm i}}% \newcommand{\iff}{\Longleftrightarrow} \newcommand{\imp}{\Longrightarrow}% \newcommand{\isdiv}{\,\left.\right\vert\,}% \newcommand{\ket}[1]{\left\vert #1\right\rangle}% \newcommand{\ol}[1]{\overline{#1}}% \newcommand{\pars}[1]{\left( #1 \right)}% \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}} \newcommand{\pp}{{\cal P}}% \newcommand{\root}[2][]{\,\sqrt[#1]{\,#2\,}\,}% \newcommand{\sech}{\,{\rm sech}}% \newcommand{\sgn}{\,{\rm sgn}}% \newcommand{\totald}[3][]{\frac{{\rm d}^{#1} #2}{{\rm d} #3^{#1}}} \newcommand{\ul}[1]{\underline{#1}}% \newcommand{\verts}[1]{\left\vert\, #1 \,\right\vert}$ $${\expo{-x/10} \over 10}\,\dd x = \dd\xi\,,\quad\xi\ \in\ \left[0,1\right)\quad\mbox{and}\quad \pars{~\xi = 0\quad\imp\quad x = 0~}$$

$$-\expo{-x/10} + 1= \xi\quad\imp\quad\color{#00f}{\large x = -10\ln\pars{1 - \xi}}$$

With $\tt C++$:

// randomExp10.cc
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
const long double RANDMAX1=((long double)RAND_MAX) + 1.0L;

// It generates 10 random numbers with distribution exp(-x/10)/10
int main()
{
long double r,x;

for ( unsigned char n = 0 ; n<10 ; ++n ) {
do r=1.0L - rand()/RANDMAX1; while (  ( r<0 ) || ( r>1.0L )  );
x=(-10.0L)*log(r);
cout<<x<<' ';
}
cout<<endl;

return 0;
}

7.8264e-05 1.41031 14.0897 6.1369 7.60928 2.47128 0.481872 11.3589 11.3724 27.2865

-