HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptModerate

Random value excluding both limits

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomexcludingvaluelimitsboth

Problem

The javascript method Math.random() returns a (pseudo-)random float between 0 (inclusive) and 1 (exclusive).

In some use cases, I need both limits to be excluded, a random float between 0 (exclusive) and 1 (exclusive).

This is what I currently use to generate said random float:

do {
    var random = Math.random();
} while(random === 0);


But I have a feeling that the (possibly) multiple draws from the random pool somehow mess up the distribution of my random floats.

Is there any downside to using this snippet and if so, how could I generate random floats while excluding both the lower and the upper limits?

Solution

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).

Code Snippets

var random = Math.random()*almost_one + Number.MIN_VALUE;
var random = Math.random()*almost_one + (1.0 - almost_one);
var random = almost_one*almost_one + (1.0 - almost_one);

Context

StackExchange Code Review Q#95656, answer score: 14

Revisions (0)

No revisions yet.