ArticleslgStudy

mathematics

Ziggurat algorithm

Ziggurat algorithm is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Ziggurat algorithm rather than just read about it. In short: The ziggurat algorithm is an algorithm for pseudo-random number sampling. Belonging to the class of rejection sampling algorithms, it relies on an underlying source of uniformly-distributed random numbers, typically from a pseudo-random number generator, as well as precomputed tables.

Ziggurat algorithm — main illustration
Ziggurat algorithm — illustration

Key takeaways

  • Ziggurat algorithm belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Ziggurat algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Ziggurat algorithm from memory before moving on to harder problems.

Reference excerpt

The ziggurat algorithm is an algorithm for pseudo-random number sampling. Belonging to the class of rejection sampling algorithms, it relies on an underlying source of uniformly-distributed random numbers, typically from a pseudo-random number generator, as well as precomputed tables. The algorithm is used to generate values from a monotonically decreasing probability distribution. It can also be applied to symmetric unimodal distributions, such as the normal distribution, by choosing a value from one half of the distribution and then randomly choosing which half the value is considered to have been drawn from. It was developed by George Marsaglia and others in the 1960s. A typical value produced by the algorithm only requires the generation of one random floating-point value and one random table index, followed by one table lookup, one multiply operation and one comparison. Sometimes (2.5% of the time, in the case of a normal or exponential distribution when using typical table sizes) more computations are required. Nevertheless, the algorithm is computationally much faster than the two most commonly used methods of generating normally distributed random numbers, the Marsaglia polar method and the Box–Muller transform, which require at least one logarithm and one square root calculation for each pair of generated values. However, since the ziggurat algorithm is more complex to implement it is best used when large quantities of random numbers are required. The term ziggurat algorithm dates from Marsaglia's paper with Wai Wan Tsang in 2000; it is so named because it is conceptually based on covering the probability distribution with rectangular segments stacked in decreasing order of size, resulting in a figure that resembles a ziggurat.

Theory of operation The ziggurat algorithm is a rejection sampling algorithm; it randomly generates a point in a distribution slightly larger than the desired distribution, then tests whether the generated point is inside the desired distribution. If not, it tries again. Given a random point underneath a probability density curve, its x coordinate is a random number with the desired distribution. The distribution the ziggurat algorithm chooses from is made up of n equal-area regions; n−1 rectangles that cover the bulk of the desired distribution, on top of a non-rectangular base that includes the tail of the distribution. Given a monotone decreasing probability density function f(x), defined for all x≥0 , the base of the ziggurat is defined as all points inside the distribution and below y1=f(x1). This consists of a rectangular region from (0,0) to (x1,y1), and the (typically infinite) tail of the distribution, where x≥x1(and y≤y1). This layer (call it layer 0) has area A. On top of this, add a rectangular layer of width x1 and height A/x1, so it also has area A. The top of this layer is at height y2=y1+A/x1 and intersects the density function at a point (x2,y2), where y2=f(x2). This layer includes every point in the density function between y1 and y2, but (unlike the base layer) also includes points such as (x1,y2) which are not in the desired distribution. Further layers are then stacked on top. To use a precomputed table of size n (n = 256 is typical), one chooses x1such that xn=0, meaning that the top box, layer n−1, reaches the distribution's peak at (0,f(0)) exactly. Layer i extends vertically in range [yi,yi+1], and can be divided into two regions horizontally: the (generally larger) portion in range [0,xi+1] which is entirely contained within the desired distribution, and the (small) portion in range [xi+1,x], which is only partially contained. Ignoring for a moment the problem of layer 0, and given uniform random variables U0 and U1∈[0,1), the ziggurat algorithm can be described as:

Choose a random layer 0≤i<n. Let x=U0xi. If x<xi+1, return x. Let y=yi+U1(yi+1−yi). Compute f(x). If y<f(x), return x. Otherwise, choose new random numbers and go back to step 1. Step 1 amounts to choosing a low-resolution y coordinate. Step 3 tests if the x coordinate is clearly within the desired density function without knowing more about the y coordinate. If it is not, step 4 chooses a high-resolution y coordinate, and step 5 does the rejection test. With closely spaced layers, the algorithm terminates at step 3 a very large fraction of the time. For the top layer n−1, however, this test always fails, because xn=0. Layer 0 can also be divided into a central region and an edge, but the edge is an infinite tail. To use the same algorithm to check if the point is in the central region, generate a fictitious x0=A/y1. This will generate points with x<x1 with the correct frequency, and in the rare case that layer 0 is selected and x≥x1, use a special fallback algorithm to select a point at random from the tail. Because the fallback algorithm is used less than one time in a thousand, speed is not essential. Thus, the full ziggurat algorithm for one-sided distributions is:

Choose a random layer 0≤i<n. Let x=U0xi. If x<xi+1, return x. If i=0, generate a point from the tail using the fallback algorithm. Let y=yi+U1(yi+1−yi). Compute f(x). If y<f(x), return x. Otherwise, choose new random numbers and go back to step 1. For a two-sided distribution, the result must be negated 50% of the time. This can often be done conveniently by choosing U0∈[−1,1] and, in step 3, testing if ∣x∣<xi+1.

Fallback algorithms for the tail Because the ziggurat algorithm only generates most outputs very rapidly, and requires a fallback algorithm whenever x>x1 it is always more complex than a more direct implementation. The specific fallback algorithm depends on the distribution. For an exponential distribution, the tail looks just like the body of the distribution. One way is to fall back to the most elementary algorithm E = −ln(U1) and let x = x1 − ln(U1). Another is to call the ziggurat algorithm recursively and add x1 to the result. For a normal distribution, Marsaglia suggests a compact algorithm:

Let x = −ln(U1)/x1. Let y = −ln(U2). If 2y > x2, return x + x1. Otherwise, go back to step 1. Since x1 ≈ 3.5 for typical table sizes, the test in step 3 is almost always successful. Since −ln(U1) is an exponentially distributed variate, an implementation of the exponential distribution may be used.

Optimizations The algorithm can be performed efficiently with precomputed tables of xi and yi = f(xi), but there are some modifications to make it even faster:

… excerpt ends here. Continue reading the full article.

Illustrations

Ziggurat algorithm: The Ziggurat algorithm used to generate sample values with a normal distribution. (Only positive values are shown for simplicity.) The pink dots are initially uniform-distributed random numbers. The desired distribution function is first segmented into equal areas "A".  One layer i is selected at random by the uniform source at the left. Then a random value from the top source is multiplied by the width of the chosen layer, and the result is x tested to see which region of the layer it falls into with 3 possible outcomes: 1) (left, solid black region) the sample is clearly under the curve and may be output immediately, 2) (right, vertically striped region) the sample value may lie under the curve, and must be tested further.  In that case, a random y value within the chosen layer is generated and compared to f(x).  If less, the point is under the curve and the value x is output.  If not, (the third case), the chosen point x is rejected and the algorithm is restarted from the beginning.
The Ziggurat algorithm used to generate sample values with a normal distribution. (Only positive values are shown for simplicity.) The pink dots are initially uniform-distributed random numbers. The desired distribution function is first segmented into equal areas "A". One layer i is selected at random by the uniform source at the left. Then a random value from the top source is multiplied by the width of the chosen layer, and the result is x tested to see which region of the layer it falls into with 3 possible outcomes: 1) (left, solid black region) the sample is clearly under the curve and may be output immediately, 2) (right, vertically striped region) the sample value may lie under the curve, and must be tested further. In that case, a random y value within the chosen layer is generated and compared to f(x). If less, the point is under the curve and the value x is output. If not, (the third case), the chosen point x is rejected and the algorithm is restarted from the beginning.

Worked examples

Example 1 — a first encounter with Ziggurat algorithm

Start with the simplest possible case. Write down what Ziggurat algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Ziggurat algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Ziggurat algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Ziggurat algorithm

In research
Ziggurat algorithm appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Ziggurat algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Ziggurat algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Non-uniform random numbers, Pseudorandom number generators, Statistical algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Ziggurat algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Ziggurat algorithm” →

How to study Ziggurat algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Ziggurat algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Ziggurat algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Ziggurat algorithm in simple terms?

The ziggurat algorithm is an algorithm for pseudo-random number sampling. Belonging to the class of rejection sampling algorithms, it relies on an underlying source of uniformly-distributed random numbers, typically from a pseudo-random number generator, as well as precomputed tables.

Why does Ziggurat algorithm matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Ziggurat algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Ziggurat algorithm.

Tags

  • Non-uniform random numbers
  • Pseudorandom number generators
  • Statistical algorithms

Keep exploring