Last active
August 30, 2023 21:25
-
-
Save Infinitusvoid/23234d28e4bf9751f46877b4a3ccc9d7 to your computer and use it in GitHub Desktop.
C++ : How to generate a random int between min max ?
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> // 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