The R software provides access to the uniform distribution by the `dunif()`

, `punif()`

, `qunif()`

and `runif()`

functions. Apply the `help()`

function on these functions for further information.

The `runif()`

function generates random deviates of the uniform distribution and is written as `runif(n, min = 0, max = 1)`

. We may easily generate `n`

number of random samples within any interval, defined by the `min`

and the `max`

argument.

```
# generate 40 random variables, uniformly distributed between -1 and 1
runif(40, min = -1, max = 1)
```

```
## [1] -0.16051593 -0.98925295 0.38704217 -0.07857278 -0.37244737
## [6] 0.57450432 0.52962437 0.36794927 0.67974969 -0.39745423
## [11] -0.50794790 0.82014626 -0.42908643 -0.52879332 0.67377003
## [16] 0.25277361 -0.54526958 0.15936296 -0.75713287 0.45327253
## [21] 0.88160116 -0.50112795 -0.75934551 0.80645625 0.68470037
## [26] -0.68296404 -0.27019276 -0.92321646 0.79570448 -0.18689250
## [31] 0.02345381 -0.08639688 -0.27130729 0.97188662 -0.91165460
## [36] 0.45593361 0.06793150 0.45563015 0.41967761 -0.55066703
```

We may approximate the density function for \(X \sim U(-2, 0.8)\) by using the `runif()`

function and plot the results as a histogram.

```
rand.unif <- runif(10000, min = -2, max = 0.8)
hist(rand.unif, freq = FALSE, xlab = 'x', density = 20)
```

Further, we plot both, the density histogram from above as well as the uniform probability distribution for the interval [-2,0.8], by applying the `dunif()`

function.

```
a <- -2
b <- 0.8
hist(rand.unif,
freq = FALSE,
xlab = 'x',
ylim = c(0, 0.4),
xlim = c(-3,3),
density = 20,
main = "Uniform distribution for the interval [-2,0.8]")
curve(dunif(x, min = a, max = b),
from = -5, to = 5,
n = 100000,
col = "darkblue",
lwd = 2,
add = TRUE,
yaxt = "n",
ylab = 'probability')
```