Skip to content

Instantly share code, notes, and snippets.

@BrianSipple
Created August 12, 2017 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianSipple/967ecb9045d6d8c65f233a16212c051d to your computer and use it in GitHub Desktop.
Save BrianSipple/967ecb9045d6d8c65f233a16212c051d to your computer and use it in GitHub Desktop.
C++ Bounded Random
#include <cstdlib>
int boundedRandom(int min, int max) {
static const double randFraction = 1.0 / (static_cast<double>(RAND_MAX));
int offset = (max - min + 1) * (rand() * randFraction);
return min + offset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment