こんばんは。初質問失礼します。
a.b.cの箱1つに車が一台入ってて、aに絶対車が入ってるのが条件で、最初にどこに入ってるか当てる人が最初の自分の意見を変えない時と変えた時の的中率を1万回から10万回やってどっちに寄るかって課題です。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/19 09:55
2018/04/19 09:56
2018/04/19 10:01
2018/04/19 10:03
2018/04/19 10:06
2018/04/19 10:09
2018/04/19 10:16
2018/04/19 10:17
2018/04/19 10:18
2018/04/19 10:19
2018/04/19 10:23
退会済みユーザー
2018/04/19 10:24
2018/04/19 10:25
2018/04/19 10:26
2018/04/19 10:26
2018/04/19 10:27
2018/04/19 10:30
2018/04/19 10:33
2018/04/19 10:35
2018/04/19 10:38
退会済みユーザー
2018/04/19 10:40
退会済みユーザー
2018/04/19 10:53
回答3件
0
Ruby
1# frozen_string_literal: true 2 3boxes = { 4 a: true, 5 b: false, 6 c: false, 7} 8 9count = 100_000 # 10万回 10 11sonomama_rate = count.times.count do 12 select_box = boxes.keys.sample 13 boxes[select_box] 14end.to_f / count 15 16naosu_rate = count.times.count do 17 select_box = boxes.keys.sample 18 next_select_box = (boxes.keys - [select_box]).sample 19 boxes[next_select_box] 20end.to_f / count 21 22monty_rate = count.times.count do 23 select_box = boxes.keys.sample 24 open_box = (boxes.keys - [select_box]) 25 .reject(&boxes.method(:[])).sample 26 next_select_box = (boxes.keys - [open_box]).sample 27 boxes[next_select_box] 28end.to_f / count 29 30monty_hall_rate = count.times.count do 31 select_box = boxes.keys.sample 32 open_box = (boxes.keys - [select_box]) 33 .reject(&boxes.method(:[])).sample 34 next_select_box = (boxes.keys - [select_box, open_box]).first 35 boxes[next_select_box] 36end.to_f / count 37 38printf("%.1f%%\n", sonomama_rate * 100) 39printf("%.1f%%\n", naosu_rate * 100) 40printf("%.1f%%\n", monty_rate * 100) 41printf("%.1f%%\n", monty_hall_rate * 100)
CoffeeScript
1boxes = 2 a: true 3 b: false 4 c: false 5 6count = 100000 # 10万回 7 8sample = (arr) -> 9 arr[Math.floor(Math.random() * arr.length)] 10 11sonomamaRate = [1..count].filter -> 12 selectBox = sample(Object.keys(boxes)) 13 boxes[selectBox] 14.length / count 15 16naosuRate = [1..count].filter -> 17 selectBox = sample(Object.keys(boxes)) 18 next_selectBox = sample(Object.keys(boxes) 19 .filter((box) -> box != selectBox)) 20 boxes[next_selectBox] 21.length / count 22 23montyRate = [1..count].filter -> 24 selectBox = sample(Object.keys(boxes)) 25 openBox = sample(Object.keys(boxes) 26 .filter((box) -> box != selectBox) 27 .filter((box) -> !boxes[box])) 28 next_selectBox = sample(Object.keys(boxes) 29 .filter((box) -> box != openBox)) 30 boxes[next_selectBox] 31.length / count 32 33montyHallRate = [1..count].filter -> 34 selectBox = sample(Object.keys(boxes)) 35 openBox = sample(Object.keys(boxes) 36 .filter((box) -> box != selectBox) 37 .filter((box) -> !boxes[box])) 38 next_selectBox = Object.keys(boxes) 39 .filter((box) -> box != selectBox && box != openBox) 40 boxes[next_selectBox] 41.length / count 42 43 44console.log "#{(sonomamaRate * 100).toFixed(1)}%" 45console.log "#{(naosuRate * 100).toFixed(1)}%" 46console.log "#{(montyRate * 100).toFixed(1)}%" 47console.log "#{(montyHallRate * 100).toFixed(1)}%"
Kotlin
1import kotlin.ranges.IntRange 2 3fun main(args: Array<String>) { 4 val boxes = mapOf( 5 "a" to true, 6 "b" to false, 7 "c" to false) 8 9 val count = 100000 // 10万回 10 11 val sonomamaRate = IntRange(1, count).count(fun(_: Int): Boolean { 12 val selectBox = boxes.keys.shuffled().first() 13 return boxes.getOrDefault(selectBox, false) 14 }).toDouble() / count 15 16 val naosuRate = IntRange(1, count).count(fun(_: Int): Boolean { 17 val selectBox = boxes.keys.shuffled().first() 18 val nextSelectBox = boxes.keys 19 .filterNot({ selectBox.equals(it) }) 20 .shuffled().first() 21 return boxes.getOrDefault(nextSelectBox, false) 22 }).toDouble() / count 23 24 val montyRate = IntRange(1, count).count(fun(_: Int): Boolean { 25 val selectBox = boxes.keys.shuffled().first() 26 val openBox = boxes.keys 27 .filterNot({ selectBox.equals(it) }) 28 .filterNot({ boxes.getOrDefault(it, false) }) 29 .shuffled().first() 30 val nextSelectBox = boxes.keys 31 .filterNot({ openBox.equals(it) }) 32 .shuffled().first() 33 return boxes.getOrDefault(nextSelectBox, false) 34 }).toDouble() / count 35 36 val montyHallRate = IntRange(1, count).count(fun(_: Int): Boolean { 37 val selectBox = boxes.keys.shuffled().first() 38 val openBox = boxes.keys 39 .filterNot({ selectBox.equals(it) }) 40 .filterNot({ boxes.getOrDefault(it, false) }) 41 .shuffled().first() 42 val nextSelectBox = boxes.keys 43 .filterNot({ selectBox.equals(it) || openBox.equals(it) }) 44 .shuffled().first() 45 return boxes.getOrDefault(nextSelectBox, false) 46 }).toDouble() / count 47 48 println("%.1f%%".format(sonomamaRate * 100)) 49 println("%.1f%%".format(naosuRate * 100)) 50 println("%.1f%%".format(montyRate * 100)) 51 println("%.1f%%".format(montyHallRate * 100)) 52}
※ 3番目のコードはKotlinです。Kotlinはシンタックス指定が対応していないっぽい。
投稿2018/04/19 11:41
編集2018/04/19 14:56総合スコア21735
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
多分、当てる人が早々に飽きる方に寄ると思います。
投稿2018/04/19 10:03
退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
こんにちは、質問の回答として、数学の理論上の説明と、それを実際にプログラミングで
試行した結果を述べたいと思います。
<理論上の説明>
ここに書くと長くなるのでモンティ・ホール問題とその解説をご覧ください。
結果だけ言うと選択肢を変えないで当たる確率が1/3,選択肢を変えて当たる確率が2/3です。
説明の訂正
色んな方に指摘を受け、勘違いしていたところを訂正しました。
最初私が書いたのは10000回の中で「選択肢を変えず当たる回数」「選択肢を変えて当たる回数」
を表示したものでした。
おそらく、「選択肢を変える」「選択肢を変えない」をそれぞれ100000回くらい繰り返し、
当たった回数を比較すべきだったと思います。
それぞれ100000回ずつ試行することで得られる結果(当たった回数)を比較することにしました。
間違ったことを書いてしまい すみませんでした。
Java
1import java.util.Random; 2 3public class Sum { 4 5 final static String BINGO = "あたり"; 6 7 public static void main(String[] args) { 8 int trycount = 100000; 9 int sum1; 10 int sum2; 11 System.out.println("各" + trycount + "回 試行します"); 12 13 sum1 = count(trycount, false); 14 sum2 = count(trycount, true); 15 16 System.out.println("はじめの選択から変更しない場合"); 17 System.out.println("当たった回数の合計: " + sum1 + "/" + trycount); 18 System.out.println("はじめの選択から変更する場合"); 19 System.out.println("当たった回数の合計: " + sum2 + "/" + trycount); 20 21 } 22 23 static int count(int tryCount, boolean rechoice) { 24 int c = 0; 25 for (int i = 0; i < tryCount; i++) { 26 boolean b = judge(rechoice); 27 if (b) 28 c++; 29 } 30 31 return c; 32 } 33 34 static boolean judge(boolean rechoice) {//戻り値がfalseなら選択せずにあたった回数が増え,trueなら選択肢を変えて当たった回数が増える 35 String[] box = box(); 36 int FirstChoice = yourChoice(3); 37 //rechoiceがtrueのときは選択を変える 38 if (rechoice) { 39 40 String []box2 = new String[2]; 41 int pos = 0; 42 for (int i = 0; i < 3; i++) { 43 if (FirstChoice != i) { 44 box2[pos] = box[i]; 45 pos++; 46 } 47 } 48 for(int i = 0; i < 2; i++){ 49 if(!box2[i].equals(BINGO)){ 50 return box2[1 - i].equals(BINGO); 51 } 52 } 53 } 54 //rechoiceがfalseは選択を変えない 55 else { 56 return box[FirstChoice].equals(BINGO); 57 58 59 } 60 return true; 61 } 62 63 static int yourChoice(int c) { 64 Random random = new Random(); 65 return random.nextInt(c); 66 } 67 68 static String[] box() { //当たりの番号をランダムに指定するなら中身のコメントん部分を使う 69 String[] box = { "ヤギ", "ヤギ", "ヤギ" }; 70 //Random random = new Random(); 71 //int i = random.nextInt(3); 72 box[0] = BINGO; 73 return box; 74 } 75}
<結果>(結果には毎回少しの誤差があります。この結果はその中の一つにすぎません)
各100000回 試行します
はじめの選択から変更しない場合
当たった回数: 33545/100000
はじめの選択から変更する場合
当たった回数: 66892/100000
また、10回の試行をして、それセットを100000回ずつ繰り返し、その合計を求めてみます。
Java
1public static void main(String[] args) { 2 int trycount = 10; 3 int try_set = 100000; 4 int c1, c2; 5 int sum1 = 0; 6 int sum2 = 0; 7 System.out.println("各 10回 試行します"); 8 System.out.println("このセットを" + try_set + "回繰り返します。"); 9 int i = 0; 10 while(i < try_set){ 11 12 c1 = count(trycount, false); 13 c2 = count(trycount, true); 14 15 /*System.out.println("*** はじめの選択から変更しない場合"); 16 System.out.println("当たった回数: " + c1 + "/" + trycount); 17 System.out.println("*** はじめの選択から変更する場合"); 18 System.out.println("当たった回数: " + c2 + "/" + trycount);*/ 19 20 sum1 += c1; 21 sum2 += c2; 22 i++; 23 } 24 25 System.out.println("はじめの選択から変更しない場合"); 26 System.out.println("当たった回数の合計: " + sum1 + "/" + trycount * try_set); 27 System.out.println("はじめの選択から変更する場合"); 28 System.out.println("当たった回数の合計: " + sum2 + "/" + trycount * try_set); 29 30 }
<結果>
各 10回 試行します
このセットを100000回繰り返します。
はじめの選択から変更しない場合
当たった回数の合計: 333379/1000000
はじめの選択から変更する場合
当たった回数の合計: 667234/1000000
つまり初めの選択から変更する場合に傾きます。
投稿2018/04/19 12:34
編集2018/04/21 02:57退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/04/19 12:35
2018/04/19 13:03
2018/04/19 13:10
2018/04/19 13:21
2018/04/19 16:37
退会済みユーザー
2018/04/20 00:48
退会済みユーザー
2018/04/20 01:00 編集
2018/04/20 01:15
退会済みユーザー
2018/04/20 01:42 編集
2018/04/20 03:17
退会済みユーザー
2018/04/20 03:50
2018/04/20 04:43
退会済みユーザー
2018/04/20 07:17
退会済みユーザー
2018/04/20 07:19 編集
2018/04/20 07:21
2018/04/20 07:27
2018/04/20 07:29
2018/04/20 07:30
2018/04/20 07:35
2018/04/20 07:35
退会済みユーザー
2018/04/20 07:38 編集
退会済みユーザー
2018/04/20 07:40
2018/04/20 07:41
退会済みユーザー
2018/04/20 07:43
2018/04/20 07:52
2018/04/20 07:54
2018/04/20 07:54
退会済みユーザー
2018/04/20 07:54
2018/04/20 08:05
退会済みユーザー
2018/04/20 08:41 編集
退会済みユーザー
2018/04/21 07:20
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。