novi - 5 months ago 28

R Question

When I run in R

`runif(100,max=0.1, min=1e-10)`

I get 100 uniformly distributed random variables

`min=1e-10`

How to generate uniform random variables on the whole interval (between min and max values).

Answer

(*Edited to replace exp(-10) with 1e-10*)

Given your max of `0.1`

and min of `1e-10`

, the probability that any given value is less than 0.1 is given by

```
(1e-4 - 1e-10) / (0.1 - 1e-10) = 9.99999e-04
```

The probability that 100 random values from this distribution are all greater than `1e-4`

is

```
(1 - 9.99999e-04) ^ 100 = 0.90479
```

About 90.5%. So you shouldn't be at all surprised that in a draw of 100 numbers from this distribution, you didn't see any less than `1e-4`

. This is expected more than 90.5% of the time theoretically. We can even verify this in simulation:

```
set.seed(47) # for replicability
# 100,000 times, draw 100 numbers from your uniform distribution
d = replicate(n = 1e5, runif(100, max = 0.1, min = 1e-10))
# what proportion of the 100k draws have no values less than 1e-4?
mean(colSums(d < 1e-4) == 0)
# [1] 0.90557
# 90.56% - very close to our calculated 90.48%
```

For more precision, we can repeat with even more replications

```
# same thing, 1 million replications
d2 = replicate(n = 1e6, runif(100, max = 0.1, min = 1e-10))
mean(colSums(d2 < 1e-4) == 0)
# [1] 0.90481
```

So, with 1MM replications, `runif()`

is almost exactly meeting expectations. It is off from the expectation by `0.90481 - 0.90479 = 0.00002`

. I would say there is absolutely no evidence that `runif`

is broken.

We can even plot the histograms for some of the replications. Here are the first 20:

```
par(mfrow = c(4, 5), mar = rep(0.4, 4))
for (i in 1:20) hist(d[, i], main = "", xlab = "", axes = F)
```

To get more very low random values, your two choices are (a) draw more numbers from the uniform or (b) change distributions to one that has more weight closer to 0. You could try an exponential distribution? Or maybe, if you want a hard upper bound as well you could scale a beta distribution? Your other choice is to not use random values at all, maybe you want evenly spaced values and `seq`

is what you're looking for?