St · Statistics & Probability

How to Generate Random Numbers From a Probability Equation

Updated 13 October 2026

At a glance

  1. 1. Define the distribution

    Specify the possible values and make sure the probability density or mass function is valid and normalized.

  2. 2. Find the cumulative distribution

    Integrate a continuous density, or add probabilities for a discrete distribution, to obtain its cumulative distribution function.

  3. 3. Draw a uniform value

    Generate U uniformly between zero and one using a random-number generator.

  4. 4. Transform or accept

    Use the inverse CDF when practical; otherwise use a suitable method such as rejection sampling.

If an equation describes a probability distribution, you can use it to generate random values that follow that distribution. The standard general method is inverse-transform sampling: turn a uniformly distributed random number into one with the desired distribution.

An equation by itself is not enough, though. You need to know the allowed values and how probability is assigned to them. If you mean a rule such as y=f(x)y=f(x), choosing random xx values and evaluating f(x)f(x) gives random outputs, but does not necessarily make those outputs follow a particular probability distribution.

Check that the equation defines a distribution

For a continuous random variable, the equation should specify a probability density function f(x)f(x). A valid density must be nonnegative and have total area 1 over its support:

f(x)≥0,∫−∞∞f(x) dx=1.f(x)\geq 0,\qquad \int_{-\infty}^{\infty} f(x)\,dx=1.

The support is the set of values the variable can take. For a discrete random variable, use a probability mass function instead: each probability must be nonnegative, and the probabilities over all possible values must sum to 1.

If a nonnegative function g(x)g(x) has a finite, nonzero integral but is not normalized, it can be made into a density by dividing by that integral. If the normalizing constant is unknown or the function is difficult to work with, a different sampling method may be needed.

Use inverse-transform sampling

Let F(x)F(x) be the cumulative distribution function (CDF), which gives the probability that a random value is at most xx. For a continuous density,

F(x)=∫−∞xf(t) dt.F(x)=\int_{-\infty}^{x} f(t)\,dt.

The inverse-transform procedure is:

  1. Generate UU uniformly on (0,1)(0,1).
  2. Find xx such that F(x)=UF(x)=U.
  3. Return that xx as the sample.

In compact form, this is X=F−1(U)X=F^{-1}(U). More generally, including for discrete distributions or CDFs with flat sections, use the generalized inverse

F−1(u)=inf⁡{x:F(x)≥u}.F^{-1}(u)=\inf\{x:F(x)\geq u\}.

This method works because the chance that the returned value is at most xx is F(x)F(x), the CDF you wanted. Some CDFs have no convenient formula for their inverse; for a continuous CDF, you can often find the value numerically by solving F(x)=UF(x)=U.

Worked example: a triangular density

Suppose the desired density is

f(x)={2x,0≤x≤1,0,otherwise.f(x)=\begin{cases} 2x, & 0\leq x\leq 1,\\ 0, & \text{otherwise}. \end{cases}

It is nonnegative, and its total area is

∫012x dx=1,\int_0^1 2x\,dx=1,

so it is a valid density. For 0≤x≤10\leq x\leq 1, its CDF is

F(x)=∫0x2t dt=x2.F(x)=\int_0^x 2t\,dt=x^2.

Set U=F(x)=x2U=F(x)=x^2 and solve for xx. Since values in the support are nonnegative, x=Ux=\sqrt{U}. Thus the sampling rule is:

  1. Generate UU uniformly between 0 and 1.
  2. Return X=UX=\sqrt{U}.

For example, if the generator returns U=0.36U=0.36, the sample is X=0.36=0.6X=\sqrt{0.36}=0.6. The result is not uniform on [0,1][0,1]: values near 1 are more likely, as specified by the density 2x2x.

When inversion is inconvenient

If the CDF can be evaluated but its inverse cannot be calculated directly, find the generalized inverse numerically, for example by monotone search; for a continuous CDF, this amounts to solving F(x)=UF(x)=U. For a discrete distribution, accumulate the probabilities and return the first value whose cumulative probability is at least UU.

Rejection sampling is another option when you can easily generate proposals from a simpler density q(x)q(x). If the target density is f(x)f(x) and a constant MM satisfies f(x)≤Mq(x)f(x)\leq Mq(x) everywhere, propose YY from qq and accept it with probability

f(Y)Mq(Y).\frac{f(Y)}{Mq(Y)}.

If you reject a proposal, repeat. This can be inefficient if many proposals are rejected. Markov chain Monte Carlo methods can be useful for more difficult distributions, but their samples are generally dependent and require additional checks.

Common mistakes

  • Treating a function value as a probability. A density value is not usually the probability of one exact outcome; probabilities come from areas under the density.
  • Ignoring normalization or support. A negative function, or one whose total integral is not finite and positive, cannot be used directly as a probability density.
  • Sampling the input uniformly and assuming the output has the desired distribution. A transformation changes the distribution. Use the CDF method or another method designed for the target distribution.
  • Expecting a closed-form answer for every equation. A valid distribution may have an inverse CDF that is not expressible with elementary functions; numerical or alternative algorithms may be needed.

A random-number generator usually produces pseudorandom values rather than truly random ones. For simulations, a standard library’s distribution-sampling functions are often preferable to implementing a method from scratch; use a cryptographic generator instead when unpredictability is the requirement.

References

  1. Luc Devroye, Non-Uniform Random Variate Generation

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.