-2

I have a function to get a random number returned from function roll_dice however it seems to be a problem when i´m calling it agian, it gives me the same number, somehow its not given a new seed when called agian.

int roll_dice(void){
  int random_dice_numbers[2];
  int i = 0;
  int sum = 0;
  srand(time(NULL));

  for(i = 0; i < 2; i++){
    random_dice_numbers[i] = rand() % 6+1;


  }

  sum = random_dice_numbers[0] + random_dice_numbers[1];
  return sum;

}

int main(void){
int sum = 0;

sum = roll_dice();
printf("You rolled", sum);

sum = roll_dice();
printf("You rolled", sum);

return 0;
}
Nicco
  • 286
  • 1
  • 2
  • 15

1 Answers1

3

time(NULL) returns seconds and your function is very fast so you are probably seeding with the same number in both runs.

You should only seed your random number generator once.

Surya
  • 15,703
  • 3
  • 51
  • 74
dohashi
  • 1,771
  • 8
  • 12