Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Note: Not good at math and my terminology may be very wrong.

I have a uniform random number generator that outputs a number between [0,1]. I'd like a function that returns a random number between 0 and 1 using an exponential or exponential-like distribution - i.e. F(random()) is more likely to be between 0.5-1.0 than 0.0-0.5.

Exponential would be great, but from attempting Inverse Transform Sampling I haven't been able to find an inverse for the exponential function so I'm not sure if this is possible. For my purposes, any increasing distribution would be valid.

share|cite|improve this question
up vote 7 down vote accepted

(Second, better answer.) Instead of using acceptance-rejection (as described in my original answer below), it would be even easier just to take $\{Y\}$, the fractional part of $Y$, where $Y$ is exponential$(\lambda)$. I can't find an online proof of it, but it is true that if $Y$ is exponential$(\lambda)$ then $\{Y\}$ has the truncated exponential$(\lambda)$ distribution on $[0,1]$, which is exactly what you're looking for. (And as I mention below, it's easy to generate exponential random variables: If $X \sim U(0,1)$, and $Y = -\ln(x)/\lambda$, then $Y$ is exponential$(\lambda)$.)

The proof that $\{Y\}$ has the truncated exponential distribution on $[0,1]$ is not that bad. You find the joint distribution $P(\lfloor Y \rfloor = k, \{Y\} \leq x)$ and then use that to find the distributions of $\lfloor Y \rfloor$ and $\{Y\}$. The distribution of $\lfloor Y \rfloor$ turns out to be geometric, that of $\{Y\}$ is the truncated exponential, and $\lfloor Y \rfloor$ and $\{Y\}$ are independent. A reference is Bennett Eisenberg, "On the expectation of the maximum of IID geometric random variables," Statistics and Probability Letters, 2008.

(Original answer.) Actually, the exponential distribution is one of the few distributions you can invert analytically when using the inverse transform sampling method.

The pdf of the exponential distribution is $f(x) = \lambda e^{-\lambda x}$, $x \geq 0$. Thus the cdf is $F(x) = \int_0^x \lambda e^{-\lambda t} dt = 1 - e^{-\lambda x}$. Inverting this by solving for $x$ we have $F(x) - 1 = - e^{-\lambda x} $ $\Rightarrow -\lambda x = \ln (1- F(x))$ $\Rightarrow F^{-1}(x) = -\ln(1-x)/\lambda$. Thus if $X$ is an observation from a $U(0,1)$ distribution, then $$Y = -\ln(1-X)/\lambda$$ is an observation from an exponential$(\lambda)$ distribution. Moreover, $X$ having a $U(0,1)$ distribution is equivalent to $1-X$ having a $U(0,1)$ distribution, so we often express the transformation more simply as $$Y = -\ln X/\lambda.$$

Since you want your random variable between $0$ and $1$, though, what you really want is a truncated exponential transform. Probably the easiest way to implement that is the following: Generate a $U(0,1)$ random variable $X$. Then use the formula $Y = - \ln X/\lambda$ to transform that to an exponentially distributed random variable. If this value is between $0$ and $1$ then use it as the output of your function. If this value is not between $0$ and $1$, try the procedure again until you do get an exponentially distributed random variable between $0$ and $1$. (This is a variation of a standard sampling technique, the acceptance-rejection method.)

share|cite|improve this answer
Ah I had tried -ln(x)/λ before but thought it wasn't working when it returned numbers greater than 1. Its interesting you can just retry until its within the needed range. Thanks! – Derek Dahmer Mar 19 '11 at 23:13
@Derek: Did you see Mike's update to the answer? – Rahul Mar 20 '11 at 6:40
@rahul-narain Yup, thats a much better option from a programming standpoint. For anyone who doesn't know what {y} means (I had to look up 'fractional part'), its the decimal part of the number, like frac(5.2) = 0.2 – Derek Dahmer Mar 22 '11 at 23:35

If $U$ is uniform on $(0,1)$ and $a$ is a parameter in $(0,1)$, $V=U^a$ has density proportional to $v^{(1-a)/a}$ on $v\in(0,1)$ hence, for any fixed small $h$, $P(V\in(v,v+h))$ is an increasing function of $v$.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.