floor, round and random
Tuesday, June 17th, 2008In my previous entry, I discussed why floor(), when used with random() is often preferable than ceil().
Now, it’s time to discuss why round() is undesirable, when used with the random() function.
The round() function will round real values, towards the nearest whole number. 10.2 is rounded downward to 10, 8.7 is rounded upwards to 9.
Values with exactly .5 after the decimal point are a special case, as they are exactly halfway between the nearest decimal point. Game Maker uses a technique called Bankers’ Rounding when rounding .5 decimal points. Essentially, half the time it rounds downward, the other half upward. Always creating an even whole number.
0.5 will be rounded down to 0, 1.5 rounded up to 2, 2.5 rounded down to 2. This form of rounding is used to reduce cumulative errors that can be introduced by a lot of rounding.
(more…)