<s>
The	O
ziggurat	B-Algorithm
algorithm	I-Algorithm
is	O
an	O
algorithm	O
for	O
pseudo-random	B-Algorithm
number	I-Algorithm
sampling	I-Algorithm
.	O
</s>
<s>
Belonging	O
to	O
the	O
class	O
of	O
rejection	B-Algorithm
sampling	I-Algorithm
algorithms	O
,	O
it	O
relies	O
on	O
an	O
underlying	O
source	O
of	O
uniformly-distributed	O
random	O
numbers	O
,	O
typically	O
from	O
a	O
pseudo-random	B-Algorithm
number	I-Algorithm
generator	I-Algorithm
,	O
as	O
well	O
as	O
precomputed	O
tables	O
.	O
</s>
<s>
Nevertheless	O
,	O
the	O
algorithm	O
is	O
computationally	O
much	O
faster	O
than	O
the	O
two	O
most	O
commonly	O
used	O
methods	O
of	O
generating	O
normally	O
distributed	O
random	O
numbers	O
,	O
the	O
Marsaglia	B-Algorithm
polar	I-Algorithm
method	I-Algorithm
and	O
the	O
Box	B-Algorithm
–	I-Algorithm
Muller	I-Algorithm
transform	I-Algorithm
,	O
which	O
require	O
at	O
least	O
one	O
logarithm	O
and	O
one	O
square	O
root	O
calculation	O
for	O
each	O
pair	O
of	O
generated	O
values	O
.	O
</s>
<s>
However	O
,	O
since	O
the	O
ziggurat	B-Algorithm
algorithm	I-Algorithm
is	O
more	O
complex	O
to	O
implement	O
it	O
is	O
best	O
used	O
when	O
large	O
quantities	O
of	O
random	O
numbers	O
are	O
required	O
.	O
</s>
<s>
The	O
term	O
ziggurat	B-Algorithm
algorithm	I-Algorithm
dates	O
from	O
Marsaglia	O
's	O
paper	O
with	O
Wai	O
Wan	O
Tsang	O
in	O
2000	O
;	O
it	O
is	O
so	O
named	O
because	O
it	O
is	O
conceptually	O
based	O
on	O
covering	O
the	O
probability	O
distribution	O
with	O
rectangular	O
segments	O
stacked	O
in	O
decreasing	O
order	O
of	O
size	O
,	O
resulting	O
in	O
a	O
figure	O
that	O
resembles	O
a	O
ziggurat	B-Application
.	O
</s>
<s>
The	O
ziggurat	B-Algorithm
algorithm	I-Algorithm
is	O
a	O
rejection	B-Algorithm
sampling	I-Algorithm
algorithm	O
;	O
it	O
randomly	O
generates	O
a	O
point	O
in	O
a	O
distribution	O
slightly	O
larger	O
than	O
the	O
desired	O
distribution	O
,	O
then	O
tests	O
whether	O
the	O
generated	O
point	O
is	O
inside	O
the	O
desired	O
distribution	O
.	O
</s>
<s>
The	O
distribution	O
the	O
ziggurat	B-Algorithm
algorithm	I-Algorithm
chooses	O
from	O
is	O
made	O
up	O
of	O
n	O
equal-area	O
regions	O
;	O
n−1	O
rectangles	O
that	O
cover	O
the	O
bulk	O
of	O
the	O
desired	O
distribution	O
,	O
on	O
top	O
of	O
a	O
non-rectangular	O
base	O
that	O
includes	O
the	O
tail	O
of	O
the	O
distribution	O
.	O
</s>
<s>
Given	O
a	O
monotone	O
decreasing	O
probability	O
density	O
function	O
f(x )	O
,	O
defined	O
for	O
all	O
x	O
≥	O
0	O
,	O
the	O
base	O
of	O
the	O
ziggurat	B-Application
is	O
defined	O
as	O
all	O
points	O
inside	O
the	O
distribution	O
and	O
below	O
y1	O
=	O
f(x1 )	O
.	O
</s>
<s>
Ignoring	O
for	O
a	O
moment	O
the	O
problem	O
of	O
layer	O
0	O
,	O
and	O
given	O
uniform	O
random	O
variables	O
U0	O
and	O
U1∈	O
[	O
0	O
,	O
1	O
)	O
,	O
the	O
ziggurat	B-Algorithm
algorithm	I-Algorithm
can	O
be	O
described	O
as	O
:	O
</s>
<s>
Thus	O
,	O
the	O
full	O
ziggurat	B-Algorithm
algorithm	I-Algorithm
for	O
one-sided	O
distributions	O
is	O
:	O
</s>
<s>
Because	O
the	O
ziggurat	B-Algorithm
algorithm	I-Algorithm
only	O
generates	O
most	O
outputs	O
very	O
rapidly	O
,	O
and	O
requires	O
a	O
fallback	O
algorithm	O
whenever	O
x>x1	O
,	O
it	O
is	O
always	O
more	O
complex	O
than	O
a	O
more	O
direct	O
implementation	O
.	O
</s>
<s>
Another	O
is	O
to	O
call	O
the	O
ziggurat	B-Algorithm
algorithm	I-Algorithm
recursively	O
and	O
add	O
x1	O
to	O
the	O
result	O
.	O
</s>
<s>
Nothing	O
in	O
the	O
ziggurat	B-Algorithm
algorithm	I-Algorithm
depends	O
on	O
the	O
probability	O
distribution	O
function	O
being	O
normalized	O
(	O
integral	O
under	O
the	O
curve	O
equal	O
to	O
1	O
)	O
,	O
removing	O
normalizing	O
constants	O
can	O
speed	O
up	O
the	O
computation	O
of	O
f(x )	O
.	O
</s>
<s>
Repeat	O
n−1	O
times	O
for	O
the	O
layers	O
of	O
the	O
ziggurat	B-Application
.	O
</s>
<s>
There	O
will	O
be	O
some	O
round-off	B-Algorithm
error	I-Algorithm
,	O
but	O
it	O
is	O
a	O
useful	O
sanity	B-Error_Name
test	I-Error_Name
to	O
see	O
that	O
it	O
is	O
acceptably	O
small	O
.	O
</s>
<s>
When	O
actually	O
filling	O
in	O
the	O
table	O
values	O
,	O
just	O
assume	O
that	O
xn	O
=	O
0	O
and	O
yn	O
=	O
f(0 )	O
,	O
and	O
accept	O
the	O
slight	O
difference	O
in	O
layer	O
1	O
's	O
area	O
as	O
rounding	B-Algorithm
error	I-Algorithm
.	O
</s>
<s>
For	O
more	O
awkward	O
distributions	O
,	O
numerical	B-Algorithm
integration	I-Algorithm
may	O
be	O
required	O
.	O
</s>
