610 Appendix B
density of the numbersy=f(x)is given by 1/f′(x), so this should be equal to the
prescribed distributionP(y):
1 /f′(x)=P(y)with (B.8a)
y=f(x). (B.8b)
We must construct a functionfthat yields the prescribed distributionP, i.e. one
that satisfies(B.8b). To this end, we use the following relation betweenfand its
inversef−^1 :
(f−^1 )′(y)f′(x)= 1 (B.9)
from which it follows that
P(y)=(f−^1 )′(y). (B.10)
There is a restricted number of distributions for which such a functionfcan be
found, because it is not always possible to find an invertible primitive function to the
distributionP. A good example for which thisispossible is the Maxwell distribution
for the velocities in two dimensions. Taking the Boltzmann factor 1/(kBT)equal
to 1/2 for simplicity, the velocities are distributed according to
P(vx,vy)dvxdvy=e−v
(^2) / 2
dvxdvy=e−v
(^2) / 2
vdvdφ=P(v)dvdφ, (B.11)
so the normvof the velocity is distributed according to
P(y)=ye−y
(^2) / 2
. (B.12)
From (B.10) we find that the functionfis defined by
f−^1 (y)=−e−y
(^2) / 2
+Const.=x (B.13)
so that
y=f(x)=
√
−2ln(Const.−x). (B.14)
Becausexlies between 0 and 1, andybetween 0 and∞, we find for the constant
the value 1, and a substitutionx→ 1 −x(preserving the interval [0,1] of allowed
values forx) enables us to write
y=f(x)=
√
−2ln(x). (B.15)
This method is very efficient since each random number generated by the uniform
generator yields a nonuniformly distributed random number.
From(B.11), we see that it is possible to generate Gaussian random numbers
starting from a distribution(B.12), since we can consider the Maxwell distribution
as a distribution for the generation of two independent Gaussian random numbers
vx=vcosφ,vy=vsinφ. From this it is clear that by generating two random
numbers, one being the valuevwith a distribution according to(B.12)and another
being the valueφwith a uniform distribution between 0 and 2π, we can construct