IT/C++
[C++] 동전의 앞면 및 뒷면 판단 예제(rand)
JSOpg
2017. 6. 5. 17:58
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int coin();
int main() {
int i, head=0, tail=0;
srand((unsigned)time(NULL));
for(i=0; i<10; i++) {
if(coin()==0) {
head++; // 앞면
}
else {
tail++;
}
}
printf("앞면 : %d\n", head);
printf("뒷면 : %d\n", tail);
return 0;
}
int coin() {
int t = rand() % 2;
if(t == 0) {
return 0; // 앞면
}
else {
return 1; // 뒷면
}
}