# How to do Uniform Distribution in R

Uniform Distribution in R, A uniform distribution is a probability distribution in which every value between a and b has the same chance of being chosen.

The formula for calculating the likelihood of getting a value between x1 and x2 on an interval from a to b is

P(obtain value between x1 and x2) = (x2 – x1) / (b – a)

## Example of a uniform distribution

The properties of the uniform distribution are as follows:

The distribution’s mean is equal to (a + b) / 2.

2 = (b – a)2 / 12 is the variance of the distribution.

The distribution’s standard deviation is equal to 2.

### Uniform Distribution in R: Syntax

We’ll use two built-in R functions to answer questions based on the uniform distribution:

dunif(x, min, max) – creates the probability density function (pdf) for the uniform distribution, where x is a random variable’s value and min and max are the distribution’s minimum and maximum numbers, 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 distribution’s minimum and maximum numbers.

Using the Uniform Distribution in R to Solve Problems.

**Example 1:-**

A train arrives at a railway station stop every 60 minutes. What are the chances that the train will arrive in 20 minutes or less if you arrive at the railway station?

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

punif(20, min=0, max=60)

[1] 0.3333333

The probability of the bus arriving in 20 minutes or less is 0.3333333.

**Example 2:-**

A goldfish’s weight is evenly spread between 35 and 45 grams. What’s the chance of getting a fish that weighs between 38 and 42 grams if you pick one at random?

Solution: Calculate the cumulative probability of a fish weighing less than 42 pounds, then deduct the cumulative probability of a fish weighing less than 38 pounds using the following syntax:

punif(42, 35, 45) – punif(38, 35, 45)

[1] 0.4

As a result, there’s a 0.4 chance the frog weighs between 38 and 42 grams.

**Example 3:-**

The duration of a Malayalam film is evenly spread between 180 and 250 minutes. What are the chances that a movie chosen at random would last longer than 200 minutes?

Solution: We can use the formula 1 – 2 to answer this question (probability that the movie is less than 200 minutes). This information is provided by:

1 – punif(200, 180, 250)

0.7142857

The odds of a randomly chosen Malayalam film lasting more than 200 minutes are 0.7142857.