Random direction generator

Okay! So i'm programming a little robot I have and I mounted a ultra sonic sensor to it, now I wondered what would it do when it came across an obstacle, if i got it to go just one direction (say right) it has the potential to keep going in circles. So I thought I would add a random number generator with a 50-50% chance. This has proven difficult. I know some c++ but not a whole lot, and I don't know how to do it. Is there a simple solution?

This has proven difficult.

In what regards?

I know some c++ but not a whole lot, and I don't know how to do it.

Do what?

Is there a simple solution?

A servo to flip a coin and a scanner to see where it landed, and which side is up.

Or, call random().

I get the impression that you want to get the robot to move around the obstacle but it gets stuck in a loop and chases its own tail? If that's the case, you could try this control logic in your program

Loop{
read sensor; // to check for object
if exist turn left
}
else {
move forward
}