View unanswered posts | View active topics It is currently Sun Jan 26, 2020 5:31 pm

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
Using the rand() function
Author Message
Moderator

Joined: Mon Oct 04, 2010 2:18 pm
Posts: 196
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 rangerand() % 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 valuestask 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);}

Sat Jun 29, 2013 1:50 pm

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

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net

Mon Jul 01, 2013 9:05 am
Professor

Joined: Sat May 18, 2013 1:24 pm
Posts: 271
Location: Olympia, WA
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).

Tue Jul 02, 2013 3:59 pm
Rookie

Joined: Wed Jun 26, 2013 2:22 pm
Posts: 11
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
 Page 1 of 1 [ 4 posts ]

Who is online

Users browsing this forum: No registered users and 1 guest

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning