It is very very difficult to generate truely random numbers, however, you can generate ones good enough for casual usage (like guess the random number games, for example) like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <cstdlib> //This contains the rand() function we need
#include <ctime> //This gives us a seed which will chance on each run (so long as you run the program only once per second or less)
#include <iostream>
int x = 0;
int main()
{
srand(time(NULL)); //Set our seed to the current time in seconds.
x = rand() % 10; //Generate a random number between 0 and 9 and assign it to x.
cout << x << endl;//Print x.
return 0;
}
rand() % n - Generates a random number from 0-n. If you do not want 0, simply do: