When I used at first srand(time(NULL))
in rollDice()
function it did not work. But when I put it in main, it works. Why is that?
Can you tell me the logic?
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int rollDice(void) {
return (1+rand()%6) + (1+rand()%6);
}
int main(void) {
int roll;
srand(time(NULL));
roll = rollDice();
printf("You rolled %d.\n", roll);
enum Gamestatus {WON,LOST,CONTINUE};
enum Gamestatus status;
while(status==CONTINUE){
printf("You are rolling again: \n");
printf("You rolled %d\n", roll = rollDice());
if (targetPoint==roll){
printf("You win!");
status=WON;
}
else if(7==roll){
printf("You lost!");
status=LOST;
}
else
status=CONTINUE;
}
return 0;
}