Better seed for rand()?

Hi when ever i use random dont really get the random numbers i need. for example i am trying to simulate a dice roll with this function:

1
2
3
4
5
6
7

int DiceRoll(){
  
 srand(time(0));
 return rand() % 6 + 1;
  
}


and this does return a randon number. However this is how i am calling the function:

1
2
3
 
  int attack = DiceRoll();
   int def = DiceRoll();


whenever i call this the numbers are always equal. I understand this is because i am using the time seed, and not enough time has passed for a new number to be calculated. Want would be a better seed for srand()? I have not done munch coding using random numbers before.

thanks
Don't srand() more than once. Put it at the start of your program then don't mess with it anymore.
You should only call srand(time(0)) once at the beginning of your program, not every time you select a random number.
Last edited on
Topic archived. No new replies allowed.