# 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$ – Stef 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.

-


$$-\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

-