高い方から
100
100
100
98
94
90
85
85
83
79
77
あと国語だけ。

今回はばらつきが少ない

モンティホール問題についてシミュレーションするプログラムを以下のように考えた。


#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main(void){
srand((unsigned)time(NULL));
while(1){
FILE *fp;
unsigned long hit1,hit2;
unsigned long i;
for(i=hit1=0;i<~(0UL);i++){
int abc=rand()%3;
int sel=rand()%3;
if(abc==sel)
hit1++;
}
printf("変えない:%10lu回 ",hit1);

for(i=hit2=0;i<~(0UL);i++){
int open;
int abc=rand()%3;
int sel=rand()%3;
switch(abc){
case 0:switch(sel){
case 0:if(rand()%2)
open=1;
else
open=2;
break;
case 1:open=2;
break;
case 2:open=1;
break;
}
break;
case 1:switch(sel){
case 1:if(rand()%2)
open=0;
else
open=2;
break;
case 0:open=2;
break;
case 2:open=0;
break;
}
break;
case 2:switch(sel){
case 2:if(rand()%2)
open=1;
else
open=0;
break;
case 1:open=0;
break;
case 0:open=1;
break;
}
break;
}
switch(open){
case 0:if(sel==1)
sel=2;
else
sel=1;
break;
case 1:if(sel==0)
sel=2;
else
sel=0;
break;
case 2:if(sel==0)
sel=1;
else
sel=0;
break;
}
if(abc==sel)
hit2++;
}
printf("変えた:%10lu回 ",hit2);
printf("比:%10.8f",(double)hit2/hit1);
putchar('\n');
fp=fopen("abc.txt","a");
fprintf(fp,"%10lu %10lu %lu\n",hit1,hit2,hit2/hit1);
fclose(fp);
}
}

ここで、abc.txtの中身について以下のような結果が得られた

1431655892 2863276995 1
1431655892 2863256551 1
1431655892 2863267000 1
1431655928 2863277164 1
1431655892 2863256460 1
1431655928 2863267160 1
1431655892 2863277091 1
1431655892 2863256450 1
1431655928 2863267200 1
1431655892 2863276949 1
1431655892 2863256417 1
1431655892 2863267329 1
1431655892 2863276720 1
1431655928 2863256346 1
1431655892 2863267467 1
1431655892 2863276797 1
1431655892 2863256413 1
1431655892 2863267282 1
1431655891 2863272851 1
1431655928 2863266951 1
1431655928 2863271441 1

さて、比率が常に2を下回るのは何が原因だろうか?


#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include"scramble.h"
#include"others.h"

void stop(void){
printf("\nPush any key.");
getch();
printf("\r%*s",13,"");
}

short main(void){
short key;

set_key(&key);

if(input_massage(&key)){
puts("\nerror");
stop();
return(1);
}
puts("\n=====cypher=====");
if(output_cypher()){
puts("\nerror");
stop();
return(1);
}
stop();
return(0);
}