Random

From ZENotes
Jump to: navigation, search

Random Moves adds something very subtle to your bot; for any animal creation they are essential I believe; let's have a look at the basic syntax:

task main()              // Start of Code
{
  while(true)            // This just tells the snippet to go on ad on forever, since ther is nothing else to counter the while statement.
  {
    OnFwd(OUT_AC, 75);   // Forward @ 75% of power
    Wait(Random(600));   // For a duration random between 0 to 0.6 secs
    OnRev(OUT_A, 75);    // Then Backwards,
    Wait(Random(400));   // For a random something between 0 and 0.4 secs.
  }                      // Mind the { and }  or it won't compile; kate would help you with colour and lines joining them
}

You'll notice this is quite simple: instead of giving a time parameter, you'll just write down Random(Field of Randomness) instead; In the above sample the 'Bot will go forward for any time between 0 to 600 millsecs, then go back for any value between 0 to 400 millisecs; That will give it a quite drunken behaviour!

If for calculation purpose, you need Real Full Blown Randowness, just write Random() this will return anything from -infinite to +infinite; it's actually your best shot at a "flip the coin" decision-making process

Randomization can be used at any place any figure would be entered, so Speed is a valid option too. And, last but not least, a random output can be a Integer too, let's re-work the simple script above to More randomness, with Integers:

int move_time, turn_time;         //Define some integers
int fwd_speed, bck_speed;         //Define some integers

task main()                       // Start of Code
{
  while(true)                     // This just tells the snippet to go on ad on forever
  {
    move_time = Random(600);      // random between 0 to 0.6 secs
    turn_time = Random(400);      // For a random something between 0 and 0.4 secs.
    fwd_speed = Random(90);       // random between 0 to 90% power
    bck_speed = Random(70);       // random between 0 to 70% power

    OnFwd(OUT_AC, fwd_speed);     // See fwd_speed = Random(90);
    Wait(move_time);              // See move_time = Random(600);
    OnRev(OUT_A, bck_speed);      // See bck_speed = Random(70); 
    Wait(turn_time);              // See turn_time = Random(400);
  }
}

That's it, the Thing will go forward at a random speed between 0 and 90% of power for a random amount of time included between 0 to 0.6 seconds, and quite the same, while with slightly different values, will happen backwards too.

Now that poses athreat: Immobilism; all this randomness may well generate values of Zero and your 'bot won'yt move, whis isn't fun.

For your next Lessons on Randomness, may I suggest you head over here, here and there?

See you again on our Not Quite Encyclopedic resource.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox