View unanswered posts | View active topics It is currently Wed Jan 18, 2017 12:05 am

Reply to topic  [ 1 post ] 
rand() function documentation, RobotC 4.X 
Author Message

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


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:

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:

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

I also had good results from:

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  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 2 guests

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.