Too long for a comment:
Please do not use the approach given in @Yann's answer:
var random = Math.random() * (1 - Number.MIN_VALUE) + Number.MIN_VALUE;
The reason is that Number.MIN_VALUE == 5E-324
. As numbers in javascript are doubles with 52 bit mantissa and the largest number smaller than 1.0 that can be represented is somewhere around 1.0 - 2^(-52)
. The expression (1 - 5e-324)
is not representable and will truncate to exactly 1.0
.
The addition of Number.MIN_VALUE
to the random value will also truncate for large parts of the number range, skewing your distribution.
The correct (and fastest) way to do this is by using the original code in OP's question (as also pointed out by @200_success's answer).
Addendum:
Even if we let: almost_one
be the largest value smaller than 1.0 and then did:
var random = Math.random()*almost_one + Number.MIN_VALUE;
then the value would still be wrong.
To achieve a uniform distribution Math.random()
must only return values with the same exponent (i.e. only the mantissa differs). Otherwise you'd have many more numbers between [0, 0.01] than you would between [0.01, 1.0].
So for any return value of Math.random()
besides 0.0
the addition of Number.MIN_VALUE
will truncate. However if Math.random()
returns 0.0 you will get the smallest value larger than 0.0 that is representable (5e-324) as your result. But then there will be a huge gap to the next number that you can get as a result, so your distribution will not be uniform any more.
The following is also wrong:
var random = Math.random()*almost_one + (1.0 - almost_one);
This can easily be seen by assuming that Math.random()
returns its maximal value which is almost_one
, then you would have:
var random = almost_one*almost_one + (1.0 - almost_one);
which is not equal to almost_one
(it's close, but not correct).