2017-09-19 3 views
0

J'ai cette variation sur le problème de Monty Hall où au lieu de choisir et commuter/ne pas commuter, c'est fait aléatoirement.Comment puis-je faire répéter ce jeu de sélection aléatoire et compter les résultats?

Je suppose que j'ai appris assez pour être capable de créer ceci afin que le sélecteur aléatoire répète 100 fois, et produise un compte de victoire et un nombre de pertes, cependant je suis bloqué au début. Tout ce que j'ai pu montrer, c'est qu'il sélectionne avec succès au hasard, mais je ne sais pas comment faire pour répéter 100 fois et compter les résultats. Je suppose que j'ai besoin d'utiliser une boucle while et winCount ++ mais je ne sais pas comment faire pour que cela fonctionne.

Appréciez toute aide!

//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

Qu'avez-vous besoin de répéter avec précision? –

+0

L'ensemble du processus de pick1 et de supprimer un prix et re-picking. Cela a-t-il du sens? – gcspurs

+0

Vous pouvez encapsuler le processus de sélection d'un élément parmi les choix d'une fonction, puis appeler la fonction dans une boucle. –

Répondre

0

Je me suis déplacé le code à un jsFiddle avec la solution ici: https://jsfiddle.net/dwthz8v1/2/ et ont console connecté la sortie, appuyez sur f12 pour jeter un oeil à la console, je crois que le code de travail que vous recherchez est ce :

//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; 
    } 
}; 

La façon dont cela fonctionne est de boucler 100 fois dans une boucle for. Chaque itération représente un jeu individuel.

Vous configurez le tableau de choix pour chaque jeu. Faites un choix, puis supprimez un choix, puis montrez combien de fois vous avez changé le choix et combien de fois vous avez changé le temps perdu