# Thread: Making A Random Number Each Time?

1. ## Making A Random Number Each Time?

Ok so I want it so whenever I put in the command roll_dice it will print out 5 random numbers. I figured I was going to run across this problem. But whenever I use the function "roll_dice" it just print's out the same number's each time, like if I print them out twice during the program. Here is the function.

Code:
```int roll_dice()
{
int dice[5];
srand(time(NULL));

dice[1]=rand()%6+1;
dice[2]=rand()%6+1;
dice[3]=rand()%6+1;
dice[4]=rand()%6+1;
dice[5]=rand()%6+1;

cout<<dice[1];
cout<<dice[2];
cout<<dice[3];
cout<<dice[4];
cout<<dice[5];
}```
It should print out 5 random number's each time I use the function, but it print's out the same ones.

2. >It should print out 5 random number's each time I use the function, but it print's out the same ones.
That's because you reseed the generator each time you call the function. srand should be called sparingly, usually only once in a short running program.

>http://members.cox.net/srice1/random/crandom.html
Hmm, I'm not sure I'd recommend that link...

3. I thought it was the link I have at my home computer that teaches better practices for getting more random numbers.... Sorry bout that. Do what Prelude said.

4. I recommend Prelude's link: http://eternallyconfuzzled.com/articles/rand.html

Read the whole thing even though your problem is related with the seeding method.

Also, roll_dice() seems to not return anything. You may want to declare it void, instead of int.

5. Yes, I just figured that out after posting...I hate when that happens, but yes it work's fine now thanks. I'll read the article as well.

6. If you haven't already noticed, you're also trying to access elements 1-5, instead of 0-4.