# Random direction generator

**URL:** <https://forum.arduino.cc/t/random-direction-generator/208646>\
**Category:** Robotics\
**Created:** [January 30, 2014, 8:28am UTC](https://forum.arduino.cc/t/random-direction-generator/208646 "2014-01-30T08:28:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 30, 2014, 8:28am UTC](https://forum.arduino.cc/t/random-direction-generator/208646/1 "2014-01-30T08:28:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 30, 2014, 10:55am UTC](https://forum.arduino.cc/t/random-direction-generator/208646/2 "2014-01-30T10:55:19Z")

</div>

> 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().

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 5, 2014, 10:27pm UTC](https://forum.arduino.cc/t/random-direction-generator/208646/3 "2014-02-05T22:27:43Z")

</div>

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  
}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 11:38am UTC](https://forum.arduino.cc/t/random-direction-generator/208646/4 "2021-05-06T11:38:43Z")

</div>


