Skip to content

Instantly share code, notes, and snippets.

@barsbek
Last active September 23, 2017 23:24
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 barsbek/e7bd78140c8d4e12e48b68af006e81a6 to your computer and use it in GitHub Desktop.
Save barsbek/e7bd78140c8d4e12e48b68af006e81a6 to your computer and use it in GitHub Desktop.
Generate random number in range
#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