Posted By

dblandin on 10/14/10


Tagged

number random function


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Desoxena


Random number within a range


 / Published in: C++
 

This function returns a random number within a range.

For example:

// assigns low and high values for range

int rangeLow = 34, rangeHigh = 78;

// computes a random number between 34 and 78

myRandomNumber = computeRandomNumber(rangeLow, rangeHigh);

  1. int computeRandomNumber(int l, int h) {
  2. int randNumber;
  3. int range = h - l;
  4. randNumber = rand() % range + l + 1;
  5. return randNumber;
  6. }

Report this snippet  

You need to login to post a comment.