플레이어와 컴퓨터에서 동일한 임의의 숫자가 계속 나타나는 이유를 알 수 없습니다. 나는 선수를 위해 한 번, 컴퓨터를 위해 두 번 주사위를 굴리고 있습니다. roll_a_dice라는 함수를 통해 그것을하고 있습니다.왜 플레이어와 컴퓨터에 동일한 난수가 계속 나타 납니까?
Plz 내 질문과 관련없는 다른 변수를 무시합니다.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int roll_a_dice (void);
int main(int argc, const char * argv[]) {
int round=1, playerScore, playerTotal, computerScore, computerTotal;
int player, computer;
do{
printf("Welcome to the Yacht game.\nLets see who is lucky!\n");
player=roll_a_dice();
computer=roll_a_dice();
printf("Player: %d – Machine: %d\n",player, computer);
}while (player!=computer||computer!=player);
while(round!=12){
round++;
}
return 0;
}
int roll_a_dice (void){
srand(time(NULL));
return 1 + rand() % 6;
}
난수 생성기를 시드하려면'srand() '를 한 번만 호출하십시오. 보통 이것은'main()'의 시작 부분에서 이루어집니다. –
문제와 관련이 없지만 동일한 조건을 두 번 테스트하는 이유는 무엇입니까? player! = computer || computer! = player'? 그것은 말이되지 않습니다. 그 중 하나를 제거하십시오 –
덕분에 일했습니다! –