0 Replies - 1611 Views - Last Post: 10 July 2011 - 07:44 PM Rate Topic: -----

#1 stackoverflow   User is offline

  • D.I.C Addict
  • member icon

Reputation: 166
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Random numbers within a range

Posted 10 July 2011 - 07:44 PM

Description: Using this function is straight forward-- you should add a declaration to your header file and add the definition to your source file. From there you simply call the function and expect a proper integer returned. Error checking is left to the caller. This function will return an random integer within a specified range. The output is inclusive-- this means from the base to the lowest (including the base and limit).
/**
* getRandomInt - returns a random integer within a specified range 
* @base: the lowest possible integer desired
* @limit: the highest possible integer desired
* @return: integer type, expects rand() to have been seeded
*/
int getRandomInt(int base, int limit)
{
  return rand() % (limit - base + 1) + base;
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1