Possible Duplicate:
How to generate a random number from within a range - C
I want to generate a random number between 0 and 4 inclusively. How can I do this in c++?
Possible Duplicate:
How to generate a random number from within a range - C
I want to generate a random number between 0 and 4 inclusively. How can I do this in c++?
You could call std::rand()
, usning the modulo operator to limit the range to the desired one.
std::rand()%5
You can also have a look at the new C++11 random number generation utilities
Just for completeness I show you a C++11 solution using the new random
facility:
#include <random>
#include <ctime>
int main() {
std::minstd_rand generator(std::time(0));
std::uniform_int_distribution<> dist(0, 4);
int nextRandomInt = dist(generator);
return 0;
}