walker_sample, a Python code 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 MIT 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 code 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 code 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:

Last revised on 05 February 2020.