You have the following piece of code:
float rand = GetRandomNumber();
return NUM_ITEMS * rand;
... which you'd expect to return a value from 0 to NUM_ITEMS-1, giving a nice equal chance of picking any item from some list at random.
The question is this:
Most random number functions return a value from 0.0 to 0.999999999999999'. (Or, to look at it another way, 1-epsilon, where epsilon is the smallest possible floating point value that can be represented).
However, Joey Badrand (that scandalous cur) has come up with a random number function for your use which returns a value between 0.0 and 1.0! And you have no choice but to use it in your function.
- What does this do to your distribution, and why is it a bad thing?
- How do you fix your function so that the output of Joey's random number generator can be used to do the Right Thing?
Simon Cooke is an occasional video game developer, ex-freelance journalist, screenwriter, film-maker, musician, and software engineer in Seattle, WA.
The views posted on this blog are his and his alone, and have no relation to anything he's working on, his employer, or anything else and are not an official statement of any kind by them (and barely even one by him most of the time).