Last active
September 23, 2017 23:24
-
-
Save barsbek/e7bd78140c8d4e12e48b68af006e81a6 to your computer and use it in GitHub Desktop.
Generate random number in range
This file contains 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 <iostream> | |
#include <random> | |
int randomInRange(int min, int max, std::mt19937 &eng) { | |
std::uniform_int_distribution<> distr(min, max); // define the range | |
return distr(eng); | |
} | |
int main() { | |
std::random_device rd; // obtain a random number from hardware | |
std::mt19937 eng(rd()); // seed the generator | |
for(int i=0; i<1000; i++) | |
std::cout << randomInRange(0, 10, eng) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment