Generate random value with a distance between them

If your range is 1000 and you need the 50 distance above or below, the right way would be to think that the range of the random next number is from 50 to 950. If the new number is greater than the old number, add 50. If less (or equal), subtract 50. If it goes over 1000 or under 0, have it wrap around. Simple forward logic, no risk for endless loop.