Efficient Probability Vector Sampling

WALKER_SAMPLE is a Python library which efficiently samples a discrete probability vector.

For outcomes labeled i = 1, 2, 3, ..., N, a discrete probability vector X is an array of N non-negative values which sum to 1, such that X[i] is the probability of outcome i.

To sample the probability vector is to produce a sequence of outcomes i1, i2, i3, ..., which are each drawn with probability corresponding to X. For a general discrete probability vector X, a single sample operation might be expected to take a time that is proportional to O(N), the number of outcomes. Walker showed that, by constructing a new data structure, it was possible to carry out a sample in time of order O(1), that is, independent of the number of possible outcomes.


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


WALKER_SAMPLE is available in a C version and a C++ version and a FORTRAN90 version and a Matlab version and a Python version.

Related Data and Programs:

PDFLIB, a Python library which evaluates Probability Density Functions (PDF's) and produces random samples from them, including beta, binomial, chi, exponential, gamma, inverse chi, inverse gamma, multinomial, normal, scaled inverse chi, and uniform.

PROB, a Python library which evaluates, samples, inverts, and characterizes a number of Probability Density Functions (PDF's) and Cumulative Density Functions (CDF's), including anglit, arcsin, benford, birthday, bernoulli, beta_binomial, beta, binomial, bradford, burr, cardiod, cauchy, chi, chi squared, circular, cosine, deranged, dipole, dirichlet mixture, discrete, empirical, english sentence and word length, error, exponential, extreme values, f, fisk, folded normal, frechet, gamma, generalized logistic, geometric, gompertz, gumbel, half normal, hypergeometric, inverse gaussian, laplace, levy, logistic, log normal, log series, log uniform, lorentz, maxwell, multinomial, nakagami, negative binomial, normal, pareto, planck, poisson, power, quasigeometric, rayleigh, reciprocal, runs, sech, semicircular, student t, triangle, uniform, von mises, weibull, zipf.


  1. Donald Knuth,
    Seminumerical algorithms,
    Addison-Wesley-Longman, 1999.
  2. Warren Smith,
    How to sample from a probability distribution,
    April 18, 2002.
  3. Alastair Walker,
    An efficient method for generating discrete random variables with general distributions,
    ACM Transactions on Mathematical Software,
    Volume 3, Number 3, September 1977, pages 253-256.

Source Code:

Examples and Tests:

You can go up one level to the Python source codes.

Last revised on 20 February 2016.