【问题标题】:How do I get this random selector game to repeat and count the results?如何让这个随机选择器游戏重复并计算结果?
【发布时间】:2017-09-19 11:43:20
【问题描述】:

我有这个关于蒙蒂霍尔问题的变体,它不是选择和切换/不切换,而是随机完成。

我应该已经学会了足够的知识来创建这个,以便随机选择器重复 100 次,并输出一个胜利计数和一个失败计数,但是我一开始就卡住了。我所能展示的只是它成功地随机选择,但不知道如何让它重复 100 倍并计算结果。我假设我需要使用 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"]);

【问题讨论】:

  • 你需要精确重复什么?
  • pick1、取奖、重新抽奖的全过程。这有意义吗?
  • 你可以把从选项中选择一个项目的过程封装在一个函数中,然后循环调用该函数。

标签: javascript loops counter simulation


【解决方案1】:

我已将代码移至 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 次。每次迭代代表一个单独的游戏。

您为每个游戏设置了选择数组。做出一个选择,然后删除一个选择,然后显示切换选择赢了多少次,切换了多少次输了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-21
    • 2020-02-28
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2014-11-15
    相关资源
    最近更新 更多