ROBOTC.net forums
http://robotc.net/forums/

Using the rand() function
http://robotc.net/forums/viewtopic.php?f=66&t=6107
Page 1 of 1

Author:  parkway [ Sat Jun 29, 2013 1:50 pm ]
Post subject:  Using the rand() function

Hey everyone,

A question was posed in Friday's class regarding the rand() function. The person wanted to know how to make their robot move for a random range of time; specifically between 0 and 4 seconds. I used the help menu in ROBOTC to show the rand() function, but I didn't think I did a very good job answering the question. With that in mind, I wanted to share the following code. The code contains comments that hopefully explains everything. If there are any other questions or comments, please feel free to chime in. Thanks,

Code:
#pragma config(Sensor, S1,     touchSensor,              sensorTouch)
#pragma config(Sensor, S2,     compassSensor,            sensorVirtualCompass)
#pragma config(Sensor, S3,     lightSensor,              sensorLightActive)
#pragma config(Sensor, S4,     sonarSensor,              sensorSONAR)
#pragma config(Motor,  motorA,          gripperMotor,       tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          rightMotor,         tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          leftMotor,          tmotorNormal, PIDControl, encoder)

/*  The percentage sign denotes the range. You can use this a couple
different ways to manipulate which range of values is being used. For
example:

rand() % 100; //returns a value from 0-99 (remember, it's zero index'd)
rand() % 100 + 1; //returns a value from 1-100 (adds one to the min/max
range
rand() % 10 + 100; //returns a value from 100-110 (range of ten, add 100
to min/max values)

There are many, many other parameters that can be used with the rand()
function (for instance, to tweak the seed values used for the random
number generation), but for basic random number generation, the rand()
function will suffice.*/

long rTime = 0;//Created a global variable.  The data type is long in order to get a bigger range of values
task main()
{

rTime = rand() % 3000 + 1000;//minimum is 1000 maximum is 4000 Add 1000 to minium (0) and maximum (3000)

motor[motorB] = 50;
motor[motorC] = 50;
wait1Msec(rTime);

}

Author:  tfriez [ Mon Jul 01, 2013 9:05 am ]
Post subject:  Re: Using the rand() function

Another quick thing on Random Numbers:

You can also use the "Random" keyword found in ROBOTC. It will generate a Random Number between 0 and 'n' - It will make programming a little simpler than using the standard "C" command 'rand'.

Code:
random(5000); //Gives you a random number between 0-5000


Learn More Here: http://www.robotc.net/education/curricu ... umbers.pdf

Author:  Ernest3.14 [ Tue Jul 02, 2013 3:59 pm ]
Post subject:  Re: Using the rand() function

Does `srand()` need to be called first? Or does the compiler do that for you?

Author:  ronald [ Mon Jul 29, 2013 9:14 am ]
Post subject:  Re: Using the rand() function

When I compiled the program, I received errors. I had to replace motorB with rightMotor and motorC with leftMotor. Compiled correctly.

I am happy now.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/