Here’s how you create seemly random numbers i C++. This will generate a random number from 1 to 10.
#include <ctime> // For time()
using namespace std;
void main()
{
srand(time(0)); // Initialize random number generator.
r = (rand() % 10) + 1;
}
For you legacy compiler users without namespace-support.
#include "cstdlib"; // Legacy for for srand() and rand()




0 Responses to “Random numbers in C++”