Ultrasonic senor working in flashes

I recently purchased an ultrasonic sensor, hooked it up to my arduino, and uploaded the following code

#define trigPin 11
#define echoPin 12

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

When i open up the serial moniter it begins to read data
10cm
10cm
10cm
10cm
10cm
10cm
10cm
10cm
10cm
which is correct. Then when i move it it still works until randomly it starts printing "out of range..."

for example

8cm
8cm
8cm
8cm
7cm
7cm
7cm
6cm
6cm
5cm
out of range
out of range

Does anyone have any ideas?

Moderator edit: code tags corrected

Have you tried using one of the libraries avilable for the device to see if it still happens? When you do get readings, are they correct (is 10cm really 10cm)?

There is a big thread in this section for a library (now at version 1.5 I think) that works very well - http://arduino.cc/forum/index.php/topic,106043.0.html

The library shown above works like a charm - i purchased the SR-04 from ebay and couldnt be happier.