# Random function problem.

Hey guys… maybe i just can’t spot the problem, or i’m just paranoic, but i have this program… it should generate a number [1,100] then according to that go forward, go right a bit or go left a bit… But no matter how i put it it seems that it goes way more on the left side then on the right side, and if i switch the numbers (right[90-100] left[80-90]) then right seems generated more times…

``````void Walk()
{
int action = random(1,101);

if(action<80)
{
if(forwardnr > 0)
{noprinting = true;}

if(!noprinting)
{Serial.println("");}
forward(255);
delay(500);

forwardnr++;

if(forwardnr == 1){Serial.println("");}
Serial.print("---");
Serial.print(" ");
}
else if (action>80 && action<=90 && forwardnr>3)
{
noprinting = false;
forwardnr = 0;

Serial.println("");
Serial.println("");
Serial.print("--RIGHT SIDEMOVE-----------------------");

serpos(45,125);

right(255);
delay(175);

serpos(90,125);

Serial.println("");
Serial.print("---------------------------------------");
}
else if (action>90 && action<=100 && forwardnr>3)
{
noprinting = false;
forwardnr = 0;

Serial.println("");
Serial.println("");
Serial.print("--LEFT SIDEMOVE------------------------");

serpos(135,125);

left(255);
delay(175);

serpos(90,125);

Serial.println("");
Serial.print("---------------------------------------");
}
``````

Print results: left
left
left
left
left
left
left
left
left
left
left
right
left
left
left
left
left
right
right

Looks like random is not very random. Does it only hit left or right 20% of the time?

Have you tried a different seed?

No, i didn’t, until you said so. I really can’t let it much more bigger or smaller, but i tried it out with 30 percent. Here are the results:

right
left left
right
left left
right
left left left
right
left

Its seems much more balanced now, but it has still sightly more left in it…and no mater how much i try, left always dominates…

I also put a print on the action-integer… full printport:

``````Beep = 900hz 300ms
Beep = 1200hz 200ms
Beep = 900hz 300ms
Beep = 1200hz 500ms

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- ---

--RIGHT SIDEMOVE73-----------------------
Servo at 45.00 degrees
Motor = 3 Command = Backward Speed = 255
Motor = 4 Command = Forward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- ---

--LEFT SIDEMOVE93-----------------------
Servo at 135.00 degrees
Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Backward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- --- --- --- --- --- ---

--LEFT SIDEMOVE100-----------------------
Servo at 135.00 degrees
Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Backward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- --- ---

--RIGHT SIDEMOVE80-----------------------
Servo at 45.00 degrees
Motor = 3 Command = Backward Speed = 255
Motor = 4 Command = Forward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- ---

--LEFT SIDEMOVE86-----------------------
Servo at 135.00 degrees
Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Backward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- --- --- ---

--LEFT SIDEMOVE91-----------------------
Servo at 135.00 degrees
Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Backward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- --- --- --- ---

--RIGHT SIDEMOVE78-----------------------
Servo at 45.00 degrees
Motor = 3 Command = Backward Speed = 255
Motor = 4 Command = Forward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- --- --- --- --- --- ---

--LEFT SIDEMOVE95-----------------------
Servo at 135.00 degrees
Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Backward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- ---

--LEFT SIDEMOVE99-----------------------
Servo at 135.00 degrees
Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Backward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- --- --- --- ---

--LEFT SIDEMOVE98-----------------------
Servo at 135.00 degrees
Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Backward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- --- ---

--RIGHT SIDEMOVE76-----------------------
Servo at 45.00 degrees
Motor = 3 Command = Backward Speed = 255
Motor = 4 Command = Forward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- ---

--LEFT SIDEMOVE95-----------------------
Servo at 135.00 degrees
Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Backward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- --- --- ---

--RIGHT SIDEMOVE83-----------------------
Servo at 45.00 degrees
Motor = 3 Command = Backward Speed = 255
Motor = 4 Command = Forward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- ---

--RIGHT SIDEMOVE72-----------------------
Servo at 45.00 degrees
Motor = 3 Command = Backward Speed = 255
Motor = 4 Command = Forward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- ---

--LEFT SIDEMOVE91-----------------------
Servo at 135.00 degrees
Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Backward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- ---

--LEFT SIDEMOVE90-----------------------
Servo at 135.00 degrees
Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Backward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- ---

--RIGHT SIDEMOVE71-----------------------
Servo at 45.00 degrees
Motor = 3 Command = Backward Speed = 255
Motor = 4 Command = Forward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- ---

--LEFT SIDEMOVE95-----------------------
Servo at 135.00 degrees
Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Backward Speed = 255
Servo at 90.00 degrees
---------------------------------------

Motor = 3 Command = Forward Speed = 255
Motor = 4 Command = Forward Speed = 255
--- --- --- --- --- ---
``````

Did you try a different seed.

PNRG's always return the same set of numbers with a given seed. You might just be unlucky.

Why don't you print the random numbers that are being generated and see if there is a clue there?