rejection_sample, a MATLAB code which demonstrates acceptance/rejection sampling.

We suppose that for A <= X <= B, we are given a probability density function PDF(X), and wish to randomly sample X. If it is not feasible to compute the cumulative density function CDF(X) and invert it to X(CDF), then acceptance/rejection sampling can provide an alternate way of carrying out the sampling.

Briefly, a comparison curve Z(X) must be determined, such that PDF(X) <= Z(X) for all A <= X <= B, and with the property that data can be uniformly sampled under the Z curve.

If that is the case, then we uniformly sample an X value under the Z curve. Then we pick an R value uniformly between 0 and Z(X). We accept X if R <= PDF(X); otherwise, we reject this X and prepare to generate and test another value.


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


rejection_sample is available in a Matlab version.

Related Data and Programs:

histogram_data_2d_sample, a MATLAB code which demonstrates how to construct a Probability Density Function (PDF) from a frequency table over a 2D domain, and then to use that PDF to create new samples.

histogram_pdf_sample, a MATLAB code which demonstrates how sampling can be done by starting with the formula for a PDF, creating a histogram, constructing a histogram for the CDF, and then sampling.

histogram_pdf_2d_sample, a MATLAB code which demonstrates how uniform sampling of a 2D region with respect to some known Probability Density Function (PDF) can be approximated by decomposing the region into rectangles, approximating the PDF by a piecewise constant function, constructing a histogram for the CDF, and then sampling.

pdflib, a MATLAB 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 MATLAB 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.

ranlib, a MATLAB code which produces random samples from Probability Density Functions (PDF's), including Beta, Chi-square Exponential, F, Gamma, Multivariate normal, Noncentral chi-square, Noncentral F, Univariate normal, random permutations, Real uniform, Binomial, Negative Binomial, Multinomial, Poisson and Integer uniform, by Barry Brown and James Lovato.


walker_sample, a MATLAB code which efficiently samples a discrete probability vector using Walker sampling.

Source Code:

Last revised on 07 March 2019.