How to Use 'runif' for Uniform Distribution in R

R Published Updated 2 mins read By Leon Wei
How to Use 'runif' for Uniform Distribution in R cover image

Quick summary

Summarize this blog with AI

Generate continuous uniform values with runif

runif() generates numeric values from a continuous uniform distribution. Use it for a quantity such as a simulated wait between two limits. For an integer outcome such as a die face, use sample.int(). Rounding a continuous draw changes the probabilities and may create outcomes you did not intend.

All examples run sequentially in base R 4.5.1. They are teaching simulations, not evidence about real waiting times or financial returns.

Set the interval and reproduce a draw

RNGkind("Mersenne-Twister", "Inversion", "Rejection")
set.seed(123)
waits <- runif(5, min = 2, max = 6)
print(round(waits, 6))
# 3.150310 5.153221 3.635908 5.532070 5.761869
stopifnot(all(waits > 2), all(waits < 6), length(waits) == 5)
set.seed(123)
stopifnot(identical(waits, runif(5, min = 2, max = 6)))

The first argument is the number of draws. The defaults are min = 0 and max = 1. Equal-length subintervals have equal probabilities in the model; a finite sample will not fill every subinterval equally. Set the seed once at the start of an experiment. Resetting it before two groups reproduces the same underlying random sequence, which is usually inappropriate for independent groups.

Simulate fair dice with discrete sampling

set.seed(2026)
rolls <- sample.int(6, size = 12, replace = TRUE)
print(rolls)
stopifnot(length(rolls) == 12, all(rolls %in% 1:6))
# replace = TRUE allows the same face on successive rolls.

Each of the six faces has probability 1/6. Twelve rolls need not contain every face or show each face twice. Avoid round(runif(n, 1, 7)): it can produce 7, and its edge outcomes have half the interval width of its interior outcomes.

# Exact interval-width calculation, not a simulation estimate:
rounding_probabilities <- c(0.5, 1, 1, 1, 1, 1, 0.5) / 6
names(rounding_probabilities) <- 1:7
print(round(rounding_probabilities, 4))
# 0.0833 0.1667 0.1667 0.1667 0.1667 0.1667 0.0833
stopifnot(isTRUE(all.equal(sum(rounding_probabilities), 1)))

Use the distribution to check a simulation

Under a uniform wait model from 2 to 6 minutes, the probability of waiting at most 3 minutes is (3 - 2) / (6 - 2) = 0.25. The midpoint is the population mean. Neither fact says that a small random sample has exactly those results.

exact_probability <- punif(3, min = 2, max = 6)
stopifnot(exact_probability == 0.25)
set.seed(2026)
simulated_waits <- runif(10000, min = 2, max = 6)
print(round(c(mean = mean(simulated_waits),
              fraction_at_most_3 = mean(simulated_waits <= 3)), 4))
stopifnot(all(simulated_waits >= 2 & simulated_waits <= 6))

Handle boundary inputs deliberately

stopifnot(length(runif(0)) == 0)
stopifnot(identical(runif(3, min = 4, max = 4), c(4, 4, 4)))
# Validate user-supplied bounds before drawing:
lower <- 2
upper <- 6
stopifnot(length(lower) == 1, length(upper) == 1,
          is.finite(lower), is.finite(upper), lower <= upper)

Reversed limits are invalid. Equal limits return that constant; an empty sample is valid when the requested count is zero. Record the seed, RNG settings and R version when exact reproducibility matters. For a discrete outcome, document the candidate values and their probabilities instead of relying on rounding.

References

Interview Prep

Begin Your SQL, Python, and R Journey

Master 230 interview-style coding questions and build the data skills needed for analyst, scientist, and engineering roles.

Related Articles

All Articles
How to Use 'countif' in R cover image
r Oct 17, 2026

How to Use 'countif' in R

Unlock the power of 'countif' in R with our comprehensive guide. Perfect for beginners looking to enhance their R programming skills.

How to Use 'abline' in R cover image
r Apr 30, 2024

How to Use 'abline' in R

Unlock the power of 'abline' function in R for data visualization; this guide covers everything from basics to advanced applications with exampl…