I've tried to get parts of my program to run in real mode and then enable range and overflow checking to get BP7.0 to pinpoint where the problems are, but I'm getting errors I can't figure out. For example, the below code excerpt sometimes returns an Error 215: Arithmetic overflow.

The variables X and Y are both Words:

[pascal]IF (X > 50) AND (Y > 50) AND (X < 50000) AND (Y < 50000) THEN BEGIN
X := X + (20 - Random(40));
Y := Y + (20 - Random(40));
END;
[/pascal]

I really don't understand how this could cause an error, since the values assigned to X and Y will always be within the range of a Word.