# Random Number Generation

Printable View

• Feb 25th 2009, 03:56 PM
vioravis
Random Number Generation
I am trying to generate random numbers with the the constraint that the maximum value is 40 and the most likely value is around 40% of the maximum value. Are there any distributions (lognormal??) that would suit this requirement? If I assumed lognormal distribution, how would I generate this numbers with the constraint mentioned.Thanks a lot.
• Feb 25th 2009, 09:35 PM
meymathis
Did you want continuous or discrete? I'll assume continuous since you mentioned the lognormal.

No lognormal won't cut it (technically) because theoretically you can get any positive number (including larger that 40).

An easy(ish) one would be triangular shaped. See Triangular distribution - Wikipedia, the free encyclopedia.

In your case, a=0, b=40, and c=40% of 40 = 16. For what follows, I'll stick in the values for a and b, but leave c as c.

To randomly generate this take the cumulative distribution function and compute it's inverse function (set expression = to y and solve for x). Then use the rand function (matlab, C, etc.), stick it into the function you just created and voila.

CDF (from wikipedia): $\displaystyle 0\leq x \leq c:\: \frac{x^2}{40c}$
$\displaystyle c\leq x \leq 40:\: 1-\frac{(40-x)^2}{40(40-c)}$

(Check my transcribing!) We will need to know what value the CDF takes at c:
$\displaystyle CDF(c) = \frac{(c)^2}{40c} = \frac{c}{40}$. This will be the where the equivalent split occurs for the inverse function. Note that the domain for the inverse function is [0,1] (CDF's always map some domain to [0,1] so the inverse does the opposite).

To inverse this function, just invert each piece (set the expression = to y and then solve for x. You get:
$\displaystyle 0 \leq y \leq \frac{c}{40}:\: \sqrt{40cy}$
$\displaystyle \frac{c}{40} \leq y \leq 1:\: 40-\sqrt{40}\sqrt{(c-40)y-c+40}$
(Check my algebra)

So now, to get your random number generator do the following. Produce a uniform random number from 0 to 1 (use rand for most languages that I know of). That number is y. Based on whether y is bigger or smaller than $\displaystyle \frac{c}{40}$ you pick the correct formula:
$\displaystyle 0 \leq y \leq \frac{c}{40}:\: \sqrt{40cy}$
$\displaystyle \frac{c}{40} \leq y \leq 1:\: 40-\sqrt{40}\sqrt{(c-40)y-c+40}$