Skip to content

Instantly share code, notes, and snippets.

@Infinitusvoid
Last active August 30, 2023 21:25
Show Gist options
  • Save Infinitusvoid/23234d28e4bf9751f46877b4a3ccc9d7 to your computer and use it in GitHub Desktop.
Save Infinitusvoid/23234d28e4bf9751f46877b4a3ccc9d7 to your computer and use it in GitHub Desktop.
C++ : How to generate a random int between min max ?
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
int generate_random_int(int min_value, int max_value)
{
srand(time(nullptr)); // seed the random number generator with the current time
int range = max_value - min_value + 1;
int random_value = rand() % range + min_value; // generate a random integer in the specified range
return random_value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment