How to generate random number integer

Hi guys. Can you explain how to generate random numbers of integer. I don't know how to do it. Please help. Thank you.
How random do the numbers need to be?

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:

rand() % n + 1.
Thank you for the quick reply. It helps a lot. And thank you for explaining it.
Topic archived. No new replies allowed.