Floating-point calculations are repeated for the same input for the same floating-point control state.
With three separate inputs, your expression should have three different possible outputs. Therefore, the only explanation is that something is changing the state of the floating point control, for example. rounding mode, precision, etc. during program execution.
If floating point arithmetic can do the calculation exactly, you don't need to round to an integer. But if you have to round, at least round to the nearest using Round
, not Trunc
.
However, this is a categorically incorrect way to perform the discrete selection task randomly from 2, 4, and 8. Do it like this:
case Random(3) of 0: Result := 2; 1: Result := 4; 2: Result := 8; end;
Another way is to put the possible outputs into an array, and then select them as follows:
Result := arr[Random(3)];
This becomes more attractive when there are more options to choose from.
The golden rule is that if you can avoid using floating point, do it. Floating point is slower and harder to reason than integer arithmetic. Use it only when necessary.
source share