Mawg
Mawg

Reputation: 40215

I have an endless loop

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

Upvotes: 0

Views: 326

Answers (4)

atk
atk

Reputation: 9324

While i don't know Delphi, I wonder what's going on with the RandomRange function. The way its written, it looks as though RandomRange is getting you a number from 0 to 200, which you are then dividing by 100 to get a number from 0 to 2. Then, you subtract 1, getting a number from -1 to 1. If I read it correctly, then you the value should stay just about the same over time.

What do you get if you trace the values in the loop conditions?

Upvotes: 1

Conrad Frix
Conrad Frix

Reputation: 52675

I'm not a delphi person so I could be way off (and I'll delete my answer if someone tells me I am) but won't the delta just as likely be away from zero negatively as it will be positively on each iteration. Or simplified just as likely to be -1 as it is +1.

If this is true won't the value of newValue over many iteration have almost no change?

Update

Or to clarify won't the sum of a lot of random numbers between -1 and 1 be very near zero.

In any case wouldn't be simpler to create a variable to hold the number of iterationsyou might want.

e.g.

repeatcount:= RandomRange(24,40)
num:0

repeat
  num := num +1
until (num = repeatcount)

or if you just want new value to be somewhere between 24 and 40

newValue := Random(24,40)

Upvotes: 1

Rob Kennedy
Rob Kennedy

Reputation: 163357

You set the value of oldValue twice, once after each loop. It looks like you really want to set the value of oldValue2 after the second loop.

Upvotes: 3

mgiuca
mgiuca

Reputation: 21377

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 expecting oldValue to 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.

Upvotes: 7

Related Questions