ziggurat_openmp, a C code which explores how the ziggurat() library may be used, in conjunction with OpenMP, to compute random numbers efficiently and correctly.

The primary worry, when computing random numbers in OpenMP, occurs when the user calls a random number generator which may have some internal memory. In that case, the internal memory may become corrupted if multiple threads are invoking the code at the same time.

A safe way to compute random numbers is to ensure that the seed value, and any other data or tables needed by the computation, are stored externally, in the user program. That way, each thread can have a separate copy of the data, and multiple threads of random number calculations can be going along simultaneously without fear of overwriting or memory contention.

The program presented here is somewhat awkward, because it wishes to verify that OpenMP can compute exactly the same data as a sequential program. To do so, we need to create separate seeds for each thread, and ensure that the threads execute the loop iterations in some predictable sequence. When the sequential program goes through the same calculations, we again have to do some acrobatics to ensure that it is doing the same things that the parallel program did.

So some of the complications in this program are there to ensure that we can exhibit the same results sequentially and in parallel. Once you are convinced of that, and see how to go about this, your own program can be set up without some of the overhead and special instructions used in this demonstration.


In the BASH shell, the program could be run with 2 threads using the commands:

        export OMP_NUM_THREADS=2


The computer code and data files described and made available on this web page are distributed under the MIT license


ziggurat_openmp is available in a C version and a C++ version and a FORTRAN90 version.

Related Data and Programs:


dijkstra_openmp, a C code which uses OpenMP to parallelize a simple example of Dijkstra's minimum distance algorithm for graphs.

FFT_OPENMP, a C code which demonstrates the computation of a Fast Fourier Transform in parallel, using OpenMP.

FUNCTIONS_OPENMP, a C code which demonstrates the behavior of a few of the OpenMP library functions.

HEATED_PLATE_OPENMP, a C code which solves the steady (time independent) heat equation in a 2D rectangular region, using OpenMP to run in parallel.

HELLO_OPENMP, a C code which prints out "Hello, world!" using the OpenMP parallel programming environment.

IMAGE_DENOISE_OPENMP, a C code which applies simple filtering techniques to remove noise from an image, carrying out the operation in parallel using OpenMP.

JACOBI_OPENMP, a C code which illustrates the use of the OpenMP application program interface to parallelize a Jacobi iteration solving A*x=b.

julia_set_openmp, a C code which produces an image of a Julia set, using OpenMP to carry out the computation in parallel.

MANDELBROT_OPENMP, a C code which generates an ASCII Portable Pixel Map (PPM) image of the Mandelbrot fractal set, using OpenMP for parallel execution.

MD_OPENMP, a C code which carries out a molecular dynamics simulation using OpenMP.

MULTITASK_OPENMP, a C code which demonstrates how to "multitask", that is, to execute several unrelated and distinct tasks simultaneously, using OpenMP for parallel execution.

MXM_OPENMP, a C code which computes a dense matrix product C=A*B, using OpenMP for parallel execution.

openmp_test, C codes which use the OpenMP application program interface for carrying out parallel computations in a shared memory environment.

POISSON_OPENMP, a C code which computes an approximate solution to the Poisson equation in a rectangle, using the Jacobi iteration to solve the linear system, and OpenMP to carry out the Jacobi iteration in parallel.

PRIME_OPENMP, a C code which counts the number of primes between 1 and N, using OpenMP for parallel execution.

QUAD_OPENMP, a C code which approximates an integral using a quadrature rule, and carries out the computation in parallel using OpenMP.

RANDOM_OPENMP, a C code which illustrates how a parallel program using OpenMP can generate multiple distinct streams of random numbers.

SATISFY_OPENMP, a C code which demonstrates, for a particular circuit, an exhaustive search for solutions of the circuit satisfy problem, using OpenMP for parallel execution.

SCHEDULE_OPENMP, a C code which demonstrates the default, static, and dynamic methods of "scheduling" loop iterations in OpenMP to avoid work imbalance.

SGEFA_OPENMP, a C code which reimplements the SGEFA/SGESL linear algebra routines from LINPACK for use with OpenMP.

ZIGGURAT, a C code which generates points from a uniform, normal or exponential distribution, using the ziggurat method.

ziggurat_inline, a C code which generates variates from the uniform, normal or exponential distributions, using an inline random number generator for very fast execution, by Marsaglia and Tsang.


  1. Philip Leong, Guanglie Zhang, Dong-U Lee, Wayne Luk, John Villasenor,
    A comment on the implementation of the ziggurat method,
    Journal of Statistical Software,
    Volume 12, Number 7, February 2005.
  2. George Marsaglia, Wai Wan Tsang,
    The Ziggurat Method for Generating Random Variables,
    Journal of Statistical Software,
    Volume 5, Number 8, October 2000, seven pages.

Source Code:

Last revised on 03 August 2020.