Ultrasonic distance sensor sometimes reports wrong distance

False reports can occur if there is ultrasonic noise in the environment, and you won't hear it.

Ultrasonic sensors are not particularly reliable and you may have to reject some measurements, for example if they disagree substantially with other recent measurements. Try taking the best two out of three, similar to a moving average.