# Thread: How to get a random number

1. ## How to get a random number

What would you do to get a random number from say, 1 to 5? I know the function is called rand(), but I don't know how to use it.

2. Ignoring all of the sticky issues that can come up:
Code:
```#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
for (int i = 0; i < 10; i++)
cout<< rand() % 5 <<endl;
}```
You'll run into the sticky issues in the following order:
1. The same random sequence each time the program is run (use srand)
2. The numbers may not be that random (don't use modulus)
3. rand sucks (get a better random number generator)

Conveniently enough, the first two are the most common, and they're answered in the FAQ. The last is a problem that you'll only run into when you're good and ready to tackle it, so no worries at this point.

3. Alright, thanks.

4. shouldn't you be using
Code:
`1+rand()%5;`
to get a range of 1..5?

5. >shouldn't you be using
I looked again at the first post, and sure enough it said 1 to 5. How I misinterpreted that as "up to 5" is beyond me.