View unanswered posts | View active topics It is currently Fri Feb 05, 2016 8:32 pm

Reply to topic  [ 4 posts ] 
Using the rand() function 
Author Message

Joined: Mon Oct 04, 2010 2:18 pm
Posts: 196
Post 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,

#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

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
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;


Sat Jun 29, 2013 1:50 pm
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 620
Post 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'.

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

Learn More Here: ... umbers.pdf

Timothy Friez
ROBOTC Developer - SW Engineer

Mon Jul 01, 2013 9:05 am
User avatar

Joined: Sat May 18, 2013 1:24 pm
Posts: 271
Location: Olympia, WA
Post Re: Using the rand() function
Does `srand()` need to be called first? Or does the compiler do that for you?

FTC Team 6424, the 'Oly Cow - Chief programmer.
FRC Team 4450, Olympia Robotics Federation (ORF).

and also quadrotors. Quadrotors!

Tue Jul 02, 2013 3:59 pm

Joined: Wed Jun 26, 2013 2:22 pm
Posts: 11
Post 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.

Mon Jul 29, 2013 9:14 am
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.