Random number within a range


/ Published in: C++

<p>This function returns a random number within a range.</p><p>For example:</p><p>// assigns low and high values for range</p><p><code>int rangeLow = 34, rangeHigh = 78;</code></p><p>// computes a random number between 34 and 78</p><p><code>myRandomNumber = computeRandomNumber(rangeLow, rangeHigh);</code></p>


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.