Contents

- 1 Uniform Distribution probabilities using R
- 2 Continuous uniform distribution
- 3 Uniform probabilities using dunif() function in R
- 4 Numerical Problem for Uniform Distribution
- 5 Uniform cumulative probability using punif() function in R
- 6 Uniform Distribution Quantiles using qunif() in R
- 7 Simulating Uniform random variable using runif() function in R
- 8 Endnote

## Uniform Distribution probabilities using R

In this tutorial, you will learn about how to use `dunif()`

, `punif()`

, `qunif()`

and `runif()`

functions in R programming language to compute the individual probabilities, cumulative probabilities, quantiles and to generate random sample for Uniform distribution.

Before we discuss R functions for Uniform distribution, let us see what is Uniform distribution.

## Continuous uniform distribution

Uniform distribution is used to real world problems that have uniform probabilistic behavior in a given interval.

A continuous random variable $X$ is said to have a continuous uniform distribution with parameters $\alpha$ and $\beta$ if its p.d.f. is given by

` $$ \begin{align*} f(x;a,b)&= \begin{cases} \frac{1}{b - a}, & a\leq x\leq b; \\ 0, & Otherwise. \end{cases} \end{align*} $$ `

where $a$ and $b$ are the parameters of Uniform distribution.

Read more about the theory and results of Uniform distribution here.

## Uniform probabilities using `dunif()`

function in R

For continuous probability distribution, density is the value of the probability density function at $x$ (i.e., $f(x)$).

The syntax to compute the probability density function for Uniform distribution using R is

`dunif(x,min=0,max=1)`

where

`x`

: the value(s) of the variable and,`min`

: lower limit of the distribution (default value is 0 in R),`max`

: upper limit of the distribution (default value is 1 in R).

The `min`

and `max`

value must be finite.

**Note:** If you do not specify the `min`

and `max`

value, R assumes the default value `min=0`

and `max=1`

(which is a standard uniform distribution).

The `dunif()`

function gives the value of standard uniform density function for given value(s) of `x`

.

## Numerical Problem for Uniform Distribution

To understand the four functions `dunif()`

, `punif()`

, `qunif()`

and `runif()`

, let us take the following numerical problem.

### Uniform Distribution Example

The daily amount of coffee, in liters, dispensed by a machine located in an airport lobby is a random variable $X$ having a continuous uniform distribution with $a=7$ and $b=10$. Find the probability that on a given day the amount, of coffee dispensed by this machine will be

(a) Find the value of the density function at $x=7.6$.

(b) Plot the graph of Uniform probability distribution.

(c) Find the probability that on a given day the amount of coffee dispensed by the machine will be at most 8.8 liters.

(d) Find the probability that on a given day the amount of coffee dispensed by the machine will be at least 8.5 liters.

(e) Find the probability that on a given day the amount of coffee dispensed by the machine will be more than 7.4 liters but less than 9.5 liters.

(f) Plot the graph of cumulative Uniform probabilities.

(g) What is the value of $c$, if $P(X\leq c) \geq 0.60$?

(h) Simulate 1000 Uniform distributed random variables with $a=7$ and $b=10$.

Let $X$ denote the daily amount of coffee, in liters, dispensed by a machine. Given that $X\sim U(a=7,b=10)$.

### Example 1: How to use ` dunif()`

function in R?

To find the value of the density function at $x=2.5$ we need to use ` dunif()`

function.

First let us define the given parameters as

```
# lower limit of the distribution
a <- 7
# upper limit of the distribution
b <- 10
```

The probability density function of $X$ is

` $$ \begin{aligned} f(x)&= \frac{1}{10 - 7},\\ &\quad\text{for }7 \leq x \leq 10. \end{aligned} $$ `

For part (a), we need to find the density function at $x=7.6$. That is $f(7.6)$.

(a) The value of the density function at $x=7.6$ is

` $$ \begin{aligned} f(7.6)&= \frac{1}{10-7}\\ &=0.3333333\\ &= 0.3333333 \end{aligned} $$ `

The above probability can be calculated using `dunif(7.6,min=7,max=10)`

function in R.

```
# Compute Uniform probability
result1 <- dunif(7.6,min=a,max=b)
result1
```

`[1] 0.3333333`

### Example 2 Visualize Uniform probability distribution

Using `dunif()`

function we can compute Uniform distribution probabilities for given `x`

, `min`

and `max`

. To plot the probability density function of Uniform distribution, we need to create a sequence of `x`

values and compute the corresponding probabilities.

```
# create a sequence of x values
x <- seq(7,10, by=0.02)
## Compute the Uniform pdf for each x
px<- dunif(x,min=a,max=b)
```

(b) Visualizing Uniform Distribution with `dunif()`

function and `plot()`

function in R:

The probability density function of Uniform distribution with given 7 and 10 can be visualized using `plot()`

function as follows:

```
## Plot the Uniform probability dist
plot(x,px,type="l",xlim=c(6,11),ylim=c(0,0.5),
lwd=3, col="red",ylab="f(x)")
title("PDF of Uniform (a=7,b=10)")
polygon(c(7,x,10),c(0,px,0),col="lightgray",border=NA)
lines(x,px,type = "l",lwd=2,col="red")
```

## Uniform cumulative probability using `punif()`

function in R

The syntax to compute the cumulative probability distribution function (CDF) for Uniform distribution using R is

`punif(q, min=0,max=1)`

where

`q`

: the value(s) of the variable,`min`

: lower limit of the distribution (default value is 0 in R),`max`

: upper limit of the distribution (default value is 1 in R).

Using this function one can calculate the cumulative distribution function of standard uniform distribution for given value(s) of `q`

(value of the variable `x`

).

### Example 3: How to use `punif()`

function in R?

In the above example, for part (c), we need to find the probability $P(X\leq 8.8)$.

(c) The probability that on a given day the amount of coffee dispensed by the machine will be at most 8.8 liters is

` $$ \begin{aligned} P(X\leq 8.8) &=\int_7^{8.8} f(x)\; dx\\ &=\frac{1}{3}\int_7^{8.8}\; dx\\ &=\frac{1}{3}[x]_7^{8.8}\\ &=\frac{1}{3}[8.8 - 7]\\ &=0.6. \end{aligned} $$ `

The same probability can be computed using R function `punif()`

as follows:

```
## Compute cumulative Uniform probability
result2 <- punif(8.8,min=a,max=b)
result2
```

`[1] 0.6`

### Example 4: How to use `punif()`

function in R?

In the above example, for part (d), we need to find the probability $P(X \geq 8.5)$.

(d) The probability that on a given day the amount of coffee dispensed by the machine will be at least 8.5 liters is

` $$ \begin{aligned} P(X\geq 8.5) &=\int_{8.5}^{10} f(x)\; dx\\ &=\frac{1}{3}\int_{8.5}^{10} \; dx\\ &=\frac{1}{3}[10-8.5] \\ &=0.5. \end{aligned} $$ `

To calculate the probability that a random variable $X$ is greater than a given number, one can use the option `lower.tail=FALSE`

in `punif()`

function.

Above probability can be calculated easily using `punif()`

function with argument `lower.tail=FALSE`

as

$P(X \geq 8.5) =\int_{8.5}^{10} f(x)\; dx$= `punif(8.5,min=a,max=b,lower.tail=FALSE)`

```
# compute cumulative Uniform probabilities
# with lower.tail False
punif(8.5,min=a,max=b,lower.tail=FALSE)
```

`[1] 0.5`

or by using complementary event as

$P(X \geq 8.5) = 1- P(X\leq 8.5)$= 1- `punif(8.5,min=a,max=b)`

```
# Using complementary event
1-punif(8.5,min=a,max=b)
```

`[1] 0.5`

### Example 5: How to use `punif()`

function in R?

One can also use `punif()`

function to calculate the probability that the random variable $X$ is between two values.

(e) The probability that on a given day the amount of coffee dispensed by the machine will be more than 7.4 liters but less than 9.5 liters can be written as $P(7.4 < X < 9.5)$.

` $$ \begin{aligned} P(7.4 < X < 9.5) &= P(X< 9.5) -P(X < 7.4)\\ &= 0.8333333 - 0.1333333\\ &= 0.7 \end{aligned} $$ `

The above probability can be calculated using `punif()`

function as follows:

```
res1 <- punif(9.5,min=a,max=b)
res2 <- punif(7.4,min=a,max=b)
result3 <- res1 - res2
result3
```

`[1] 0.7`

### Example 6: Visualize the cumulative Uniform probability distribution

Using `punif()`

function we can compute Uniform cumulative probabilities (CDF) for given `x`

and `rate`

. To plot the CDF of Uniform distribution, we need to create a sequence of `x`

values and compute the corresponding cumulative probabilities.

```
# create a sequence of x values
x <- seq(7,10, by=0.02)
## Compute the Uniform pdf for each x
Fx <- punif(x,min=a,max=b)
```

(f) Visualizing Uniform Distribution with `punif()`

function and `plot()`

function in R:

The cumulative probability distribution of Uniform distribution with given `x`

, `min`

and `max`

can be visualized using `plot()`

function as follows:

```
## Plot the Uniform probability dist
plot(x,Fx,type="l",xlim=c(6,11),ylim=c(0,1),
lwd=3, col="red",ylab="F(x)")
title("CDF of U(7,10)")
polygon(c(7,x,10),c(0,Fx,0),col="lightgray",border=NA)
lines(x,Fx,type = "l",lwd=2,col="red")
```

## Uniform Distribution Quantiles using `qunif()`

in R

The syntax to compute the quantiles of Uniform distribution using R is

`qunif(p,min=0,max=1)`

where

`p`

: the value(s) of the probabilities,`min`

: lower limit of the distribution (default value is 0 in R),`max`

: upper limit of the distribution (default value is 1 in R).

The function `qunif(p,min=0,max=1)`

gives $100*p^{th}$ quantile of standard uniform distribution for given value of `p`

.

The $p^{th}$ quantile is the smallest value of Uniform random variable $X$ such that $P(X\leq x) \geq p$.

It is the inverse of `punif()`

function. That is, inverse cumulative probability distribution function for Uniform distribution.

### Example 7: How to use `qunif()`

function in R?

In part (g), we need to find the value of $c$ such a that $P(X\leq c) \geq 0.60$. That is we need to find the $60^{th}$ quantile of given Uniform distribution.

```
a <- 7
b<- 10
prob <- 0.60
```

```
# compute the quantile for Uniform dist
qunif(0.60,min=a,max=b)
```

`[1] 8.8`

The $60^{th}$ percentile of given Uniform distribution is 8.8.

### Visualize the quantiles of Uniform Distribution

The quantiles of Uniform distribution with given `p`

, `min`

and `max`

can be visualized using `plot()`

function as follows:

```
p <- seq(0,1,by=0.02)
qx <- qunif(p,min=a,max=b)
# Plot the Quantiles of Uniform dist
plot(p,qx,type="l",lwd=2,col="darkred",
ylab="quantiles",
main="Quantiles of Uniform(7,10)")
```

## Simulating Uniform random variable using `runif()`

function in R

The general R function to generate random numbers from Uniform distribution is

`runif(n,min=0,max=1)`

where,

`n`

: the sample observations,`min`

: lower limit of the distribution (default value is 0 in R),`max`

: upper limit of the distribution (default value is 1 in R).

The function `runif(n,min=0,max=1)`

generates `n`

random numbers from standard uniform distribution.

### Example 8: How to use `runif()`

function in R?

In part (h), we need to generate 1000 random numbers from Uniform distribution with given parameters $min = 7,max=10$.

(h) We can use `runif(1000,min=a,max=b)`

function to generate random numbers from Uniform $U(7,10)$ distribution.

```
## initialize sample size to generate
n <- 1000
# Simulate 1000 values From Uniform dist
x_sim <- runif(n,min=a,max=b)
```

The below graphs shows the density of the simulated random variables from Uniform $U(7,10)$ Distribution.

```
## Plot the simulated data
plot(density(x_sim),xlab="Simulated x",ylab="density",
lwd=5,col="darkred",
main="Simulated data from Uniform(7,10) dist")
```

If you use same function again, R will generate another set of random numbers from $U(7,10)$.

```
# Simulate 1000 values From Uniform dist
x_sim_2 <- runif(n,min=a,max=b)
```

```
## Plot the simulated data
plot(density(x_sim_2),xlab="Simulated x",ylab="density",
lwd=5,col="blue",
main="Simulated data from U(7,10) dist")
```

For the simulation purpose to reproduce same set of random numbers, one can use `set.seed()`

function.

```
# set seed for reproducibility
set.seed(1457)
# Simulate 1000 values From Uniform dist
x_sim_3 <- runif(n,min=a,max=b)
```

```
## Plot the simulated data
plot(density(x_sim_3),xlab="Simulated x",ylab="density",
lwd=5,col="darkred",
main="Simulated data from U(7,10) dist")
```

```
set.seed(1457)
# Simulate 1000 values From Uniform dist
x_sim_4 <- runif(n,min=a,max=b)
```

```
## Plot the simulated data
plot(density(x_sim_4),xlab="Simulated x",ylab="density",
lwd=5,col="darkred",
main="Simulated data from U(7,10) dist")
```

Since we have used `set.seed(1457)`

function, R will generate the same set of Uniform distributed random numbers.

The histogram of randomly simulated values from $U(7,10)$ distribution as as follows:

```
hist(x_sim_4,breaks = 30,
main="Simulated Values from U(7,10)")
```

To learn more about other discrete and continuous probability distributions using R, go through the following tutorials:

**Discrete Distributions Using R**

Binomial distribution in R

Poisson distribution in R

Geometric distribution in R

Negative Binomial distribution in R

Hypergeometric distribution in R

**Continuous Distributions Using R**

Exponential distribution in R

Normal distribution in R

Log-Normal distribution in R

Beta distribution in R

Gamma distribution in R

Cauchy distribution in R

Laplace distribution in R

Logistic distribution in R

Weibull distribution in R

## Endnote

In this tutorial, you learned about how to compute the probabilities, cumulative probabilities and quantiles of Uniform distribution in R programming. You also learned about how to simulate a Uniform distribution using R programming.

To learn more about R code for discrete and continuous probability distributions, please refer to the following tutorials:

Probability Distributions using R

Let me know in the comments below, if you have any questions on Uniform Distribution using R and your thought on this article.