Home Â» The Uniform Distribution in R

# The Uniform Distribution in R

AÂ uniform distributionÂ is a probability distribution in which every value between an interval fromÂ aÂ toÂ bÂ is equally likely to be chosen.

The probability that we will obtain a value between x1Â and x2Â on an interval fromÂ aÂ toÂ bÂ can be found using the formula:

P(obtain value between x1Â and x2)Â  =Â  (x2Â â€“ x1) / (b â€“ a)

The uniform distribution has the following properties:

• The mean of the distribution isÂ Î¼Â = (a + b) / 2
• The variance of the distribution isÂ Ïƒ2Â = (b â€“ a)2Â / 12
• The standard deviation of the distribution isÂ ÏƒÂ =Â âˆšÏƒ2

## Uniform Distribution in R: Syntax

The two built-in functions in R weâ€™ll use to answer questions using the uniform distribution are:

dunif(x, min, max)Â â€“ calculates the probability density function (pdf) for the uniform distribution whereÂ xÂ is the value of a random variable, andÂ minÂ and maxÂ are the minimum and maximum numbers for the distribution, respectively.Â

punif(x, min, max)Â â€“ calculates the cumulative distribution function (cdf) for the uniform distribution whereÂ xÂ is the value of a random variable, andÂ minÂ and maxÂ are the minimum and maximum numbers for the distribution, respectively.Â

Find the full R documentation for the uniform distribution here.

## Solving Problems Using the Uniform Distribution in R

Example 1:Â A bus shows up at a bus stop every 20 minutes. If you arrive at the bus stop, what is the probability that the bus will show up in 8 minutes or less?

Solution:Â Since we want to know the probability that the bus will show up in 8 minutes or less, we can simply use the punif() function since we want to know the cumulative probability that the bus will show up in 8 minute or less, given the minimum time is 0 minutes and the maximum time is 20 minutes:

`punif(8, min=0, max=20)`
`## [1] 0.4`

The probability that the bus shows up in 8 minutes or less is 0.4.

Example 2:The weight of a certain species of frog is uniformly distributed between 15 and 25 grams. If you randomly select a frog, what is the probability that the frog weighs between 17 and 19 grams?

Solution:Â To find the solution, we will calculate the cumulative probability of a frog weighing less than 19 pounds, then subtract the cumulative probability of a frog weighing less than 17 pounds using the following syntax:

```punif(19, 15, 25) - punif(17, 15, 25)
```
`## [1] 0.2`

Thus, the probability that the frog weighs between 17 and 19 grams is 0.2.

Example 3:Â The length of an NBA game is uniformly distributed between 120 and 170 minutes. What is the probability that a randomly selected NBA game lasts more than 150 minutes?

Solution:Â  To answer this question, we can use the formula 1 â€“ (probability that the game lasts less than 150 minutes). This is given by:

`1 - punif(150, 120, 170)`
`## [1] 0.4`

The probability that a randomly selected NBA game lasts more than 150 minutes isÂ 0.4.