ZetCode

R probability distributions

last modified November 19, 2026

A probability distribution describes how likely each value of a random variable is. R provides the same four functions for every common distribution, distinguished by a one-letter prefix: d for the density or mass, p for the cumulative probability, q for the quantile, and r for a random draw. Once the pattern is clear for the normal distribution, it applies unchanged to the uniform, the binomial, the Poisson, and dozens of others. This article covers those four prefixes, the normal and uniform distributions, the binomial and Poisson counts, and a small simulation.

The four prefixes

The normal distribution has dnorm, pnorm, qnorm, and rnorm. The density at zero is the height of the bell curve there, the cumulative probability below zero is the area to the left, the quantile is the value with a given area to its left, and the random draw simulates observations. The seed makes the random part reproducible.

four.R
set.seed(42)
cat("density at 0:", dnorm(0), "\n")
cat("probability below 0:", pnorm(0), "\n")
cat("97.5% quantile:", qnorm(0.975), "\n")
print(round(rnorm(5), 2))

The density at zero is 0.3989, which is not a probability; a continuous distribution assigns probability to intervals, not to single points. The cumulative probability below zero is exactly 0.5 because the standard normal curve is symmetric around zero. The 97.5 percent quantile is 1.96, the value familiar from confidence intervals, and the five draws are five simulated observations.

$ Rscript four.R
density at 0: 0.3989423 
probability below 0: 0.5 
97.5% quantile: 1.959964 
[1]  1.37 -0.56  0.36  0.63  0.40

Density and probability

For a continuous variable, the probability that the value falls between two numbers is the area under the density curve between them, which pnorm computes as the difference of two cumulative probabilities. The density itself can be larger than one, because it is a height and not a probability.

normal.R
x <- c(-2, -1, 0, 1, 2)
print(dnorm(x))
print(pnorm(x))
print(pnorm(2) - pnorm(-2))

The density is highest at zero and falls symmetrically on both sides. The cumulative probabilities start at 0.0228 at minus two and reach 0.9772 at plus two; their difference is 0.9545, so about 95 percent of a normal sample lies within two standard deviations of the mean. The function pnorm(2) - pnorm(-2) is the general recipe for an interval.

$ Rscript normal.R
[1] 0.05399097 0.24197072 0.39894228 0.24197072 0.05399097
[1] 0.02275013 0.15865525 0.50000000 0.84134475 0.97724987
[1] 0.9544997

Quantiles and intervals

qnorm reverses pnorm: given a probability, it returns the value below which that fraction of the distribution lies. The central 95 percent interval therefore runs from the 2.5 percent quantile to the 97.5 percent quantile, which are symmetric around zero.

quantiles.R
print(qnorm(c(0.5, 0.9, 0.95, 0.975, 0.99)))
q <- qnorm(0.975)
cat("95% interval:", -q, "to", q, "\n")

The median is 0, the 95th percentile is 1.645, and the 99th percentile is 2.326. The 97.5 percent quantile is 1.96, so the central interval is minus 1.96 to plus 1.96. These numbers assume the standard normal distribution, with mean zero and standard deviation one; for a variable with mean 50 and standard deviation 10, multiply by 10 and add 50.

$ Rscript quantiles.R
[1] 0.000000 1.281552 1.644854 1.959964 2.326348
95% interval: -1.959964 to 1.959964 

Simulation and the empirical rule

A simulation draws many values and checks the distribution by counting. The theoretical rule for normal data says that about 68 percent of the values lie within one standard deviation of the mean and about 95 percent within two. A large sample should reproduce those proportions closely.

empirical.R
set.seed(1)
x <- rnorm(10000, mean = 50, sd = 10)
cat("mean:", round(mean(x), 2), "\n")
cat("sd:", round(sd(x), 2), "\n")
cat("within 1 sd:", round(mean(abs(x - 50) < 10), 3), "\n")
cat("within 2 sd:", round(mean(abs(x - 50) < 20), 3), "\n")

The sample mean of 49.93 and standard deviation of 10.12 are close to the requested 50 and 10. The observed proportions are 0.676 within one standard deviation and 0.95 within two, which match the 68 and 95 percent of the empirical rule. Ten thousand draws are enough to see the shape; a hundred would leave visible sampling noise.

$ Rscript empirical.R
mean: 49.93 
sd: 10.12 
within 1 sd: 0.676 
within 2 sd: 0.95 

The uniform distribution

The uniform distribution spreads probability evenly over an interval. On the default interval from zero to one, the density is one everywhere inside and zero outside, the cumulative probability at x is simply x, and the quantile at p is p. The random draws fill the interval without favouring any part of it.

uniform.R
set.seed(7)
print(dunif(c(0, 0.5, 1)))
print(punif(c(0.25, 0.5, 0.75)))
print(qunif(c(0.25, 0.5, 0.75)))
print(round(runif(5, min = 1, max = 6), 2))

The density is one at all three points, and the cumulative probabilities equal their inputs. The quantiles also equal their probabilities on this standard interval. The five draws lie between one and six because min and max were set, which is the R idiom for simulating a die roll; rounding those values would give the six faces.

$ Rscript uniform.R
[1] 1 1 1
[1] 0.25 0.50 0.75
[1] 0.25 0.50 0.75
[1] 5.94 2.99 1.58 1.35 2.22

The binomial distribution

The binomial distribution counts successes in a fixed number of independent trials, each with the same probability. dbinom gives the probability of exactly k successes, pbinom gives the probability of at most k, and rbinom simulates the counts.

binomial-dist.R
print(dbinom(0:5, size = 5, prob = 0.5))
print(pbinom(2, size = 5, prob = 0.5))
print(qbinom(c(0.25, 0.5, 0.75), size = 5, prob = 0.5))
set.seed(3)
print(rbinom(10, size = 10, prob = 0.3))

With five fair coin flips, the probability of exactly two heads is 0.3125, and the probabilities are symmetric around the middle. The cumulative probability of at most two heads is exactly 0.5, because the distribution is symmetric and two is the median. The quartiles of the count are 2, 2, and 3, so half of the experiments produce two or fewer heads. The simulated counts come from ten experiments with ten trials each and a success probability of 0.3.

$ Rscript binomial-dist.R
[1] 0.03125 0.15625 0.31250 0.31250 0.15625 0.03125
[1] 0.5
[1] 2 2 3
 [1] 2 4 3 2 3 3 1 2 3 3

The Poisson distribution

The Poisson distribution counts events in a fixed interval when they occur independently at a constant average rate. It has a single parameter, lambda, which is both the mean and the variance. The functions dpois, ppois, qpois, and rpois follow the same pattern as the others.

poisson-dist.R
print(dpois(0:5, lambda = 2))
print(ppois(2, lambda = 2))
set.seed(11)
x <- rpois(10000, lambda = 3)
cat("mean:", round(mean(x), 3), "\n")
cat("variance:", round(var(x), 3), "\n")

With an average of two events, the probability of zero events is 0.135 and the most likely count is one or two, both at 0.271. The probability of at most two events is 0.677. In the simulation, the mean of 3.024 and the variance of 2.994 both estimate the requested rate of three, which is the property that distinguishes the Poisson from distributions whose variance is independent of the mean.

$ Rscript poisson-dist.R
[1] 0.13533528 0.27067057 0.27067057 0.18044704 0.09022352 0.03608941
[1] 0.6766764
mean: 3.024 
variance: 2.994 

A complete example

The next script simulates the scores of forty students from a normal distribution with mean 70 and standard deviation 12. It reports the centre and the spread of the simulated sample, the proportion of students who reached the passing score of 60, and three quantiles.

report.R
set.seed(2026)
scores <- round(rnorm(40, mean = 70, sd = 12))
cat("n:", length(scores), "\n")
cat("mean:", round(mean(scores), 1), "\n")
cat("sd:", round(sd(scores), 1), "\n")
cat("pass rate:", mean(scores >= 60), "\n")
print(quantile(scores, probs = c(0.1, 0.5, 0.9)))

The simulated mean is 69.5 and the standard deviation is 12.1, close to the model values. Eighty-five percent of the students pass, and ninety percent of the scores fall below 86.2. Because the seed is fixed, the script produces these numbers on every run; changing the seed produces a different sample from the same distribution.

$ Rscript report.R
n: 40 
mean: 69.5 
sd: 12.1 
pass rate: 0.85 
 10%  50%  90% 
57.9 70.0 86.2 

Next steps

The descriptive statistics article summarises observed data without assuming a distribution, and the random numbers article covers sampling and seeds in more detail. The base graphics article draws the histogram that a simulated sample produces, and the vectorization article explains why rnorm(10000) is one call rather than a loop.

Source

An Introduction to R: probability distributions

R documentation: the normal distribution

R documentation: the uniform distribution

R documentation: the binomial distribution

R documentation: the Poisson distribution

In this article we have used the d, p, q, and r functions of four distributions. We read densities and cumulative probabilities from the normal curve, turned probabilities into quantiles, and checked the empirical rule on a simulated sample. We drew from the uniform, binomial, and Poisson distributions and confirmed that the Poisson variance tracks its mean. The final example simulated an exam and reported its pass rate and quantiles. The series continues with R base graphics.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all R Language tutorials.