【问题标题】:Pick random items from JavaScript array without duplicates [duplicate]从 JavaScript 数组中选择随机项而不重复 [重复]
【发布时间】:2014-09-27 22:06:39
【问题描述】:

我正在尝试从数组中获取随机数而不重复但使用 var newx = Math.floor(Math.random() * array.length);确实随机化,但它是按长度而不是数组内部的,所以它确实倾向于重复。

<html>
    <head>

    </head>
    <!-- <body> -->
        <button onclick="myFunction()">Difficulty</button>
        <p id="demo">here</p>
        <p id="test"></p>
        <script>



function myFunction() {

      function shuffle(o){ //try this shuffle function
          for(var j, g, t = o.length; t; j = Math.floor(Math.random() * t), g = o[--t], o[t] = o[j], o[j] = g);
          return o;
      };

    var i;    var l;    var y;    var n;
    var newx;   var newy;
    var useranswer;   var amountX;    var largest;
    var copyAmountX;
    var mixAlot;

    var x = parseInt(prompt("What is the first number?"+" "+"(up to 12)"));

    if (x < 13) {
       y = prompt("what is the second number?"+" "+"(choose up to 12)");
       n = prompt("how many problems to be solved?");
       amountX = []

       // adds to an array equal to x (user input)
       if (!amountX.length) {
        for (var s = 0; s <= x; s++) {
            amountX.push(s);
        }
       };
       // just to let me know if it is working. Will be taken out.
       alert(amountX);
        largest = Math.max.apply(Math, amountX);
       alert(largest);
       alert(isNaN(x));
    }
    else {
        alert("Refresh page and restart with numbers under 12")
    };
    if (y > 12 == true) {
        alert("Refresh page and restart with numbers under 12")
    };

    i = 0;
    l = amountX.length;
    copyAmountX = amountX;
    // where the core magic of everything happens.
    while (x < 13 && y < 13 && i<n) {
      newx = shuffle(copyAmountX);
      newy = Math.floor(Math.random() * y);
      useranswer = prompt("Multiply "+newx+" by "+newy)
        if (useranswer == newx * newy) {
            alert("Correct! problem "+(i+1)+" of "+n);
        };
        if (amountX == 0) {alert("You have completed your Difficulty! Good Game"); n = 0;
        };
        i++;
    };
};
</script>


    </body>
</html>

【问题讨论】:

  • 我将代码贴出来,它会提示用户。

标签: javascript arrays random


【解决方案1】:

如果您尝试从数组中获取随机数,那么我会推荐一种不同的方法:复制数组,然后随机播放副本。

function shuffle(o){ //try this shuffle function
    for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

这样,你可以不断地从洗牌的数组中弹出元素;它们将永远是随机的,并且只会出现一次。


现在关于您的代码,恐怕它有很多错误:...我删除了这部分

修复:

我更正了您的程序,现在它运行良好。请仔细阅读并应用我在 javascript 中评论的更改。 Link

【讨论】:

  • 如何将它添加到我的脚本中?
  • 请阅读我刚刚添加的关于您代码中的错误的部分。
  • 我更新了它,但它现在不起作用。我也完成了 codecademy.com 上的 javascript 课程,目前正在 teamtreehouse.com 上工作。我正在尝试检查所有内容。
  • 它不起作用,因为您在编辑代码时犯了一些错误。阅读我的新编辑。 (我可能会离线一段时间,所以如果你的代码不能再次运行,只需检查几次,注释你的代码部分,几次尝试后你应该能够弄清楚)
  • 我更新了代码。它看起来更好,但不是 1 个数字作为随机结果,而是给出例如:1,4,3,0,2 x 1 作为数学问题。
猜你喜欢
  • 2016-02-01
  • 2019-01-28
  • 1970-01-01
  • 2015-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
  • 2015-04-07
相关资源
最近更新 更多