2017-09-19 16 views
0

私はモンティホール問題にこのようなバリエーションがあります。ピッキングとスイッチングの代わりに、ランダムに行われます。このランダムセレクタゲームを繰り返し実行して結果を数える方法を教えてください。

私はおそらくランダムセレクタが100回を繰り返し、しかし、私が先頭にこだわって、勝利数と損失数を出力するように、これを作成することができるように十分なことを学びました。私が見ることができたのは、それが無作為に選択されたことですが、100xを繰り返して結果を数える方法についてはわかりません。私はwhileループとwinCount ++を使う必要があると思っていますが、この作業をどうやって行うのか分かりません。

ありがとうございました!

//define prizes 
//1 is a win 
var prizes = [1,2,3]; 
var winCount = 0; 
var loseCount = 0; 

//copy prizes array so can be reused 
var choices = prizes.slice(); 

//randomly select a prize 
var pick1 = choices[Math.floor(Math.random() * choices.length)]; 

//find index of pick1 
var pick1Index = choices.indexOf(pick1); 

//remove an item that is not the win or pick1 

function removePrize(choices,pick1){ 
    var prizeRemoved = Math.floor(Math.random()*choices.length); 
    if(choices[prizeRemoved]==pick1){ 
     return removePrize(choices,pick1); 
    } 
    else if (choices[prizeRemoved]==1){ 
     return removePrize(choice,pick1);} 
    else{ 
     return prizeRemoved; 
    } 
}; 

//randomly re-select from remaining prizes 
var pick2 = choices[Math.floor(Math.random() * choices.length)]; 


//display pick2 to show selector works 
alert(window["pick2"]); 
+0

何をpercisely繰り返す必要がない失われたどのように多くの選択肢を獲得したスイッチング時間とどのように多くの時間をスイッチング時間を示して? –

+0

pick1の全体のプロセスと賞品の削除と再ピッキング。それは理にかなっていますか? – gcspurs

+0

関数内の選択肢からアイテムを選択するプロセスをカプセル化し、関数をループで呼び出すことができます。 –

答えて

0

私はここ溶液でjsfiddleにコードを移動した:https://jsfiddle.net/dwthz8v1/2/とコンソールは、コンソールを見てみることをF12キーを押しての出力を記録している、私はあなたが探している作業コードはこれですと信じて:

//define prizes 
//1 is a win 
var prizes = [1,2,3]; 
var winCount = 0; 
var loseCount = 0; 
var totalGames = 100; 

for(var i = 0; i < totalGames; i++) { 
    //copy prizes array so can be reused 
    var choices = prizes.slice(); 
    //randomly select a prize 
    var pick1 = choices[Math.floor(Math.random() * choices.length)]; 
    var pick1Index = choices.indexOf(pick1); 


    var removedPrize = removePrize(choices, pick1); 
    var indexOfPrizeToRemove = choices.indexOf(removedPrize); 
    //remove prize from choices 
    choices.splice(indexOfPrizeToRemove); 

    //remove pick1 from choices 
    choices.splice(indexOfPrizeToRemove); 

    var pick2 = choices[Math.floor(Math.random() * choices.length)]; 

    if(pick2 == 1) { 
    console.log("Pick 2 won!"); 
    winCount++; 
    } 
    else { 
    console.log("Pick 2 was wrong"); 
    loseCount++; 
    } 
} 

console.log("Pick 2 won a total of " + winCount + " times"); 
console.log("Pick 2 lost a total of " + loseCount + " times"); 


//find index of pick1 
var pick1Index = choices.indexOf(pick1); 

//remove an item that is not the win or pick1 

function removePrize(choices,pick1){ 
    var prizeRemoved = Math.floor(Math.random()*choices.length); 
    if(choices[prizeRemoved]==pick1){ 
     return removePrize(choices,pick1); 
    } 
    else if (choices[prizeRemoved]==1){ 
     return removePrize(choices,pick1);} 
    else{ 
     return prizeRemoved; 
    } 
}; 

forループで100回ループする方法です。各反復は個々のゲームを表します。

あなたは各ゲームの選択肢の配列を設定します。選択を行い、その後、選択肢を削除し、その後、

関連する問題