Better seed for rand()?

Sep 17, 2011 at 1:06am
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
Sep 17, 2011 at 1:07am
Don't srand() more than once. Put it at the start of your program then don't mess with it anymore.
Sep 17, 2011 at 1:07am
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 Sep 17, 2011 at 1:08am
Topic archived. No new replies allowed.