Created
August 12, 2017 11:08
-
-
Save BrianSipple/967ecb9045d6d8c65f233a16212c051d to your computer and use it in GitHub Desktop.
C++ Bounded Random
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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