View unanswered posts | View active topics It is currently Wed Jun 03, 2020 10:55 pm

 Page 1 of 1 [ 1 post ]
 Print view Previous topic | Next topic
rand() function documentation, RobotC 4.X
Author Message
Rookie

Joined: Tue Apr 07, 2015 10:36 pm
Posts: 2
rand() function documentation, RobotC 4.X
The documentation for the rand() function (RobotC 4.X for LEGO Documentation > General C Programming > Math) here:

http://help.robotc.net/WebHelpMindstorms/index.htm#Resources/topics/General_C_Programming/Math.htm

says:

 Quote:A typical way to generate pseudo-random numbers in a determined range using rand is to use the modulo of the returned value by the range span and add the initial value of the range: ( value % 100 ) is in the range 0 to 99 ( value % 100 + 1 ) is in the range 1 to 100 ( value % 30 + 1985 ) is in the range 1985 to 2014

and provides the sample code:

 Code:task main(){ int min = -100;   // create int variable 'min' and set it to -100 int max = 100;    // create int variable 'max' and set it to 100 srand(nSysTime);  // generate seed for rand() from current system time   while(true)// infinite loop: {  motor[rightMotor] = (rand() % (max-min)) + min; // set 'rightMotor' to a random number in the range: [min, max]  motor[leftMotor]  = (rand() % (max-min)) + min; // set 'leftMotor' to a random number in the range: [min, max]  wait1Msec(500); // wait 500 milliseconds }}

The rand() function does not behave in this way on my system, so this seems platform-dependent or possibly just incorrect: the documentation indicates that the type of the return value from rand() is word, which it indicates may return values more than 32767, meaning it can be more than 16 bits. On my system, it seems to be. The int data type is 16 bits on my system, and this seems to be standard across all platforms for RobotC 4.X as far as I know. Why this matters:

When I read the example code above, with min = -100 and max = 100, I'd expect a random value of -100 to 99 based on the description above (so actually in [min,max) rather than [min,max]). However, that is not what I get. What happens is that rand() is generating integers that are longer than 16 bits, i.e. larger than 32767. When these integers are included in a calculation with 16 bit integers, the value is truncated to 16 bits, and is therefore negative about half of the time. The % operator returns negative results for negative inputs, so (rand() % 200) returns values from -200 to 199, not 0 to 199. So, when -100 is added to it, the result ends up between -300 to 99 instead of -100 to 99.

So, to be platform-independent, the sample code could be changed to:

 Code:abs(rand() % (max-min)) + min;

I also had good results from:

 Code:random(max-min) + min;

though this doesn't help the documentation for rand() at all.

Thank you for all that you do!
Dr. J

Thu Oct 01, 2015 10:38 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 1 post ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

 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