Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

stats.uniform

scipy.stats.uniform represents a continuous uniform distribution. Its standard support is [0, 1]; loc shifts the lower endpoint and scale sets the interval width.

How it works

scipy.stats.uniform is a continuous distribution object with support from loc through loc + scale. Use its inherited distribution methods for density, probabilities, quantiles, moments, and random variates; calling it with parameters creates a frozen distribution that retains them.

Syntax

scipy.stats.uniform(loc=0, scale=1)

Parameters

NameTypeDescription
locarray_like, optionalLocation parameter; the lower endpoint of the distribution's support. Defaults to 0.
scalearray_like, optionalScale parameter; the support width. The upper endpoint is loc + scale; defaults to 1.
sizeint or tuple of ints, optionalNumber or shape of random variates returned by rvs; defaults based on input arguments.
momentsstr, optionalRequested moments for stats, selected using letters such as m, v, s, and k.

Returns

A frozen continuous random-variable object when called with parameters; distribution methods return values appropriate to the selected operation.

Key points

  • scipy.stats.uniform models a continuous random variable whose density is constant over its interval.
  • With default parameters, the distribution covers the unit interval from zero through one.
  • Its probability density is flat inside the support and zero outside it.
  • The distribution provides the standard continuous-distribution API through inheritance.
  • Parameterizing the distribution produces a reusable frozen object with fixed settings.

Common mistakes

  • scale is the interval width, so the upper endpoint is computed as loc + scale, not supplied independently.
  • Calling uniform returns a frozen random-variable object; use that object when repeatedly evaluating the same parameterized distribution.

Example: Evaluate and sample a shifted uniform distribution

from scipy.stats import uniform

x = uniform(loc=2, scale=3)
print(x.pdf(3.5))
print(x.cdf(4.0))
print(x.rvs(random_state=0, size=3))

This creates a frozen uniform distribution on [2, 5], evaluates its density and cumulative probability, and draws three reproducible samples.

When to use it

Continuous interval modeling

Modeling a quantity with equal density across a bounded interval.

Randomized testing

Generating random test inputs within configurable bounds.

Distribution analysis

Computing densities, cumulative probabilities, quantiles, and moments for a uniform model.

Interview questions

QHow do loc and scale change the support of scipy.stats.uniform?

The standard distribution is uniform on [0, 1]. loc shifts the lower endpoint, while scale determines the interval width, producing support [loc, loc + scale].

Follow-up: How would you represent a uniform distribution on [a, b]? Use loc=a and scale=b-a, assuming b is greater than a.

QWhich kinds of operations can be performed with uniform?

The object inherits generic methods from rv_continuous, including methods for probabilities, density, moments, quantiles, and random variates. For example, pdf, cdf, ppf, stats, and rvs can be used on the distribution object.

Follow-up: Why use a frozen object? It stores the chosen parameters once, making repeated method calls less error-prone and easier to read.

QWhat is a frozen uniform random variable?

Calling the distribution with location and scale parameters creates a frozen continuous random-variable object. Its methods then use those fixed parameters automatically.

Follow-up: What must be checked when converting [a, b] into loc and scale? Ensure the width b-a is positive.

  • scipy.stats.rv_continuous
  • scipy.stats.uniform.pdf
  • scipy.stats.uniform.cdf
  • scipy.stats.uniform.ppf
  • scipy.stats.uniform.rvs
  • scipy.stats.uniform.stats

Source: scipy.stats.uniform — SciPy v1.18.0 Manual. Checked 2026-10-15.