Why do languages' random number generators tend to return a value exclusive of the upper bound of the range?

For example, an implicit range -

JavaScript's random() method "Return a random number between 0 (inclusive) and 1 (exclusive)"

An explicitly specified range -

.NET's System.Random.Next(minValue, maxValue): maxValue: The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue

1 Answer 1


Both Java and .net tend to favour this form of range (which is generally known as a half open range) in all circumstances, eg they also use it for selecting ranges in substring operations, and in operations on sections of arrays. The main reason for favouring it is that it means that the calculation end - start gives a result of the number of items included in the range (e.g, for a range of 1 up to 7, 7-1 = 6 values, I.e. 1, 2, 3, 4, 5, 6) when working with integers.

There is also another reason for floating point random number generators. Such a generator usually works by assigning random bits to the mantissa and leaving the exponent set to zero. This results in a value between 0 and 1-2^(-b) inclusive, where b is the number of bits in the mantissa, I.e it forms a half open range between 0 and 1. This is far simpler than having to include 1 in the range because it would require a special case in the generation code.

A further benefit is that when a floating point number of this kind is multiplied by an integer and rounded by truncation, the result has an even distribution. If it could generate 1, then in the very unlikely case that that actually happened, it would round to the maximum value, but not for any other outcome. This would be very hard to work with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.