The density of my points x ∈ [0,R] is exponential: ρ(x)~e^x How can I sample N points from there?
-
This is more of a statistics question than a programming question. You want to take the uniform distribution from a random function (drand48), and transform that uniform distribution into an exponential distribution. – ChuckCottrill Oct 10 '13 at 02:13
-
1A couple of comments that you may think are nit-picking but that make a huge difference in what the answer is: 1) What's commonly called the exponential distribution has density `exp(-lambda * x) / lambda`, where `lambda > 0`. That minus sign is really important! 2) The range of the exponential distribution is `[0,\infty]`, if you have an upper bound of `R` you have a truncated exponential which alters the method of generating. 3) If you meant what you said and the density is proportional to `exp(x)`, you'll have to scale it to get an area of 1. So what is it you really want here? – pjs Oct 10 '13 at 15:28
2 Answers
Use inverse sampling: you generate uniform distributed values and map them to the output of the cdf of your distribution.

- 20,967
- 7
- 57
- 109
-
1To spell it out for this case: generate a uniformly distributed value in 0-1 and take its logarithm. – Joni Oct 10 '13 at 05:52
-
@Joni That won't quite do it. If the density is growing exponentially you'll need to scale the uniform to keep it in the range [0,R]. If it's exponentially decaying, you'll need the negative of the logarithm and additional scaling to keep it in the specified range. – pjs Oct 11 '13 at 19:19
Taking your request at face value, if you want a density function that grows exponentially for x ∈ [0,R] the cumulative distribution function turns out to be (exp(x) - 1) / (exp(R) - 1)
. To generate this via inversion, set the CDF equal to a Uniform(0,1)
and solve for x
. The inversion turns out to be:
ln(1 + (exp(R) - 1) * U)
where U represents a call to the Uniform(0,1) PRNG.
If what you actually want is a truncated form of what most probability folks know as the exponential distribution, we need to determine an upper bound for the random number corresponding to your truncation point R
. In that case, the inversion is:
-ln(1 - [1 - exp(-lambda * R)] * U) / lambda
As before, U represents a call to the Uniform(0,1) PRNG. This will generate exponentials at rate lambda
, truncated at a max of R
.

- 18,696
- 4
- 27
- 56