Why does Rnd work this way?

BlitzMax Forums/BlitzMax Beginners Area/Why does Rnd work this way?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

The floating point value Rnd(a,b) is in the range from a to b, where a is included but b is excluded. This is the way it works in all versions of Blitz and all other languages I can think of.

It is a natural consequence of the way floats work. Imagine a simplified system with exactly three decimal digits. To generate a random float we generate three random digits. There are one thousand possible values uniformly distributed in the range 000 to 999. Now we put a decimal point at the front to get values from .000 to .999, which includes 0 but not 1.

So that's our random x from 0 to 1, with 1 exluded. For the range a to b we generate x as before, then calculate ( a + x * ( b - a ) ). This ranges from a to b ,with b excluded.

That's exacly how Rnd() works, except with random binary bits instead of random decimal digits.