# Random number generation inside an interval based on cdf (Zipf and Exponential)

Consider for example the Exponential distribution with c.d.f. $F(x) = 1-e^{-\lambda x}$.

$F^{-1}(x)$ would be inverse cdf (quantile function). If I generate y=F−1(x) with x uniformily distributed on [0,1], then y will follow..

If I generate $y = F^{-1}(x)$ with x uniformly distributed on $0 \lt x \lt 1$, then y will follow the specific distribution.

So I have an idea but i don't know if it is realizable:

For different values of x, where $0 \lt x \lt 1$, if we can get 1000 random numbers with the equation, $y= F^{-1}(x)$. (lets assume, we get 10,4,5,77,2,11,10,....). Can we generate this 1000 random values in interval (for example) 0-100? Hence, whether it is possible to generate random numbers "inside an interval" for exponential (and other distributions, in particular Zipf distribution).

UPDATE: If we consider the Zipf Distribution that has (from Wikipedia): $$pdf(x) = \frac{1}{x^s \times H_{N,s}}$$ $$cdf(x) = \frac{H_{x,s}}{H_{N,s}}$$

where $s \gt 0$ is the scale parameter, H is the Harmonic number $H_{N,s} = \sum_{i=1}^N \frac{1}{i^s}$

How can i generate a random generator "inside an interval" (for example from 1 to 100)? Thank you

• Question is unclear for me. 1. By $F^-(x)$ you mean $F^{-1}(x)$? 2. "If I assume... " should read: "If I generate $y=F^{-1}(x)$ with $x$ uniformily distributed on [0,1], then $y$ will follow..." 3. The final paragraph is confusing; do you want to generate values restricted to an interval? ("within a limit" means "inside an interval"??) That would amount to generate random numbers following a modified (truncated) density. – leonbloy Jul 15 '11 at 16:23
• yes you are right, i'm sorry the english is not my motherlanguage. I modified the question: it is as you 've already understood. – Maurizio Jul 15 '11 at 16:37

The quantile function $x = Q(q)$ maps unit interval into the domain of your distribution, which is $\mathbb{R}^+$ in your case. One can perform the truncation of this distribution to any interval measurable with your distribution. Let it be $a<=x<b$. Then the c.d.f. of the truncated distribution will be $F_{[a,b)}(x) = \chi_{[a,b)}(x) (F(x)-F(a) )/(F(b)-F(a))$, where the $\chi$ is the indicator function for the interval. It is also easy to find quantile of this truncated distribution. $Q_{[a,b)}(q) = Q(F(a) + q*(F(b)-F(a))$. This last statement gives your algorithm to generate random numbers.

Here is the simulation experiment for standard exponential truncated to interval $[0,2)$. EDIT

Quantile of Zipf distribution is not available in closed form, but can be numerically computed by finding the $$Q(q) = \mathop{sup} \{ x \in \mathcal{D} : cdf(x) \leq q \}$$

Once this is implemented, you can generate random variates from the truncated Zipf distribution (albeit the method is not very efficient) as so:

lb=1; ub=6;
di = ZipfDistribution[3/2];
fa = CDF[di, lb-1]; fb = CDF[di, ub];
Q[q_] := Quantile[di, fa + q*(fb - fa)] • do you mean that if until now i was using this random generator for Exponential: $-\frac{1}{\lambda} * ln(1 - RandomDouble())$, from now i should use something like this: $-\frac{1}{\lambda} * ln(1 - (F(a) + RandomDouble() * (F(b)-F(a))))$ ? (where RandomDouble() generates random values between 0 and 1) and i will get random value between a=0 and b=2 exponentially distributed? – Maurizio Jul 15 '11 at 14:41
• Yes, this way you will get random variates distributed according to exponential distribution truncated to 0..2 interval – Sasha Jul 15 '11 at 15:38
• Ok thank you very much. I think that this is applicable to every kind of distribution, isn't it? In particular I have to apply the same process also to the Zipf distribution.. – Maurizio Jul 15 '11 at 15:45
• Yes, you can apply similar ideas to discrete distributions as well. – Sasha Jul 15 '11 at 17:05
• I 've updated the question, because i'm not able to apply this procedure to the Zipf distribution! – Maurizio Jul 25 '11 at 9:32

If you wish to generate random numbers following a given density $f_x(x)$ (CDF $F_x(x)$) but restricted to an interval $[a,b]$, that would mean that you are interested in another random variable $y$ that follows the distribution of $x$ truncated to that range. This is simply the same density, properly normalized:

$$f_y(y) = \frac{1}{\int_a^b f_x(x) dx} f_x(y) \;\;, \;\; y\in[a,b]$$

From this, you can compute the CDF (on $[a,b]$ it's the same function, up to a scaling-offset), then compute it's inverse, and generate the random number using this modified quantile function.

Alternatively, you can also just generate $x$, and return if it falls inside the interval, otherwise keep trying until that happens. The resulting generate number will follow the truncated density (but this procedure will only be efficient if the probability that $x$ falls into that interval is not very small).

• Thank you. Do you mean that from your formula $F_y(y)$ i have to calculate the "new" p.d.f. version based on the original Exponential p.d.f., then i should calculate the "new" c.d.f. through the integral of p.d.f. right? – Maurizio Jul 16 '11 at 9:59
• I've updated the question :) – Maurizio Jul 25 '11 at 12:12