Why does multiplying n by a random number give you a random number between 0 and n?

978 Views Asked by At

I have been following the tutorial at Codecademy

I see that, as part of the process of creating a virtual die we multiply 'a random number between 0 and 1' by 6 to give us a random number between 0 and 6.

Could you break this logic down and explain why this works?

In short I am looking at this answer and asking for a nice simple explanation for why that works.

2

There are 2 best solutions below

4
On

The minimum of the random number is zero. $0*6=0$. The maximum of the random number is one. $1*6=6$. the numbers were uniform from the minimum to the maximum, and they still are.

1
On

Let $X$ be a random number uniformly distributed in $[0,1]$. This means that:

$$\text{for any } x \in [0,1], P(X \leq x) = x$$

Now observe that, for any $x \in [0,6]$,

$$P(6X \leq x) = P\left(X \leq \frac{x}{6}\right) = \frac{x}{6}$$

This means that $6X$ is uniformly distributed in $[0,6]$. Observe that

$$P(6X \leq x) = \int_{0}^{x}{\frac{1}{6}dx}$$

In other words, $6X$ has constant density 1/6 in $[0,6]$. That's why it's called a uniform distribution.