newValue := oldValue;
repeat
delta := (RandomRange(0, 200) / 100) - 1;
newValue := newValue + delta;
until (newValue > 24) and (newValue < 40);
oldValue := newValue;
newValue2 := oldValue2;
repeat
delta := (RandomRange(0, 200) / 100) - 1;
newValue2 := newValue2 + delta;
until (newValue2 > 24) and (newValue2 < 40) and (newValue2 < newValue);
oldValue2 := newValue2;
after a few iterations, this hits an endless loop in the second loop. It is meant to change a Float randomly by -1 to +1 and keep it in the range 24 to 40 while still being less than another Float which is being randomly changed in the same way.
Who can be first to make me say “d’oh!”? (probably by (newValue2 < newValue))
d’oh!
Well, now that it is pointed out, the answer is obvious. newValue := oldValue + delta;, not ` newValue := newValue + delta;’, so that the code reads (similar for both loops)
newValue := oldValue;
repeat
delta := (RandomRange(0, 200) / 100) - 1;
newValue := oldValue + delta; <==== **NOT** newValue
until (newValue > 24) and (newValue < 40);
oldValue := newValue;
Thanks, all, and lots of +1 all round
What do you mean by “keep it in the range 24 to 40”? Your condition “
until (newValue > 24) and (newValue < 40)” implies that it will stop once it is in that range; it will go forever if it is outside that range.The chances of it terminating depend upon
oldValue. What values are you expectingoldValueto have?In any case, such a loop is not guaranteed to terminate. You are changing the number randomly each time, so there is no guarantee it will move into the termination range at all. In particular, a large number of random numbers between -1 and 1 all added together will usually sum to approximately 0, so you can’t expect the number to change significantly over time. It’s probably the case that it happens never to enter that range.