随机数生成/混洗问题

时间:2018-05-01 18:08:35

标签: javascript arrays random numbers unique

好的,我正在尝试创建一个应用程序,当你单击一个按钮时,它生成并显示1到76之间的唯一数字的连接序列。我有它随机生成1 -78但没有欺骗但我不确定至于如何制作它,当它显示它时,它显示1个数字,然后每次点击增加+1。

首先点击[28] 第二次单击[28,33],依此类推,不重复。这是我到目前为止的代码

window.onload = onclick;

function onclick() {
    document.getElementById("BtnCall").onmousedown = GenNumber;
}


function GenNumber() {
    var num = LoadNumbers(1, 76);
    num = shufflearray(num);
    for (i = 0; i < 1; i++) {
        ShowArray(num);
    }
};


function LoadNumbers(min, max) {
    var arr = [];
    for (var i = min; i <= max; i++) {
        arr.push(i);
    }
    return arr;
}

function shufflearray(input) {
    var out = [];
    while (input.length > 0) { 
        var i = Math.random() * input.length;
        var a = input.splice(i, 1);
        out.push(a);
    }

    return out;
}

function ShowArray(m) {

    for (var i = 0; i < m.length; i++) {
		document.getElementById("usednum").innerHTML += (m[i]+', ');
    }
}

感谢您的支持/帮助:)

需要表现/喜欢这样 https://gyazo.com/bebb7c58c402934050be8bc9be29e183

而不是这个: This happens with one single click

1 个答案:

答案 0 :(得分:0)

我认为您依次使用GenNumber()LoadNumbers()shufflearray()方法等现有代码,使逻辑 TOO 一个接一个地进行随机数连接。我只是发布一个示例答案,它将满足你想要做的事情,因为&#34; 需要表现得像这样&#34; (如果我是你,那么我会尝试这种方式)。 顺便说一下,我也发布了现有代码的答案。

&#13;
&#13;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  <title>Generate Random Number</title>
</head>

<body>

  <p id="show_random"></p>
  <script type="text/javascript">
    var randoms = [];

    function makeid() {
      var randomnumber = Math.ceil(Math.random() * 76);
      randoms.push(randomnumber);
      //console.log(randoms);
      document.getElementById('show_random').innerHTML = randoms.join(' ');
    }
  </script>
  <input type="button" style="font-size:9pt" value="Gen Random" onclick="makeid()">
  </input>
  </form>
</body>

</html>
&#13;
&#13;
&#13;

使用您的代码查看

&#13;
&#13;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Generate Random Number</title>
</head>
<body>

<p id="usednum"></p>
<script type="text/javascript">
   
window.onload = onclick;

function onclick() {
    document.getElementById("BtnCall").onmousedown = GenNumber;
}


function GenNumber() {
    var num = LoadNumbers(1, 76);
    num = shufflearray(num);
    var single = Math.ceil(Math.random() * num.length);
    ShowArray(single); //with you existing code you're passing whole array
};


function LoadNumbers(min, max) {
    var arr = [];
    for (var i = min; i <= max; i++) {
        arr.push(i);
    }
    return arr;
}

function shufflearray(input) {
    var out = [];
    while (input.length > 0) { 
        var i = Math.random() * input.length;
        var a = input.splice(i, 1);
        out.push(a);
    }

    return out;
}

function ShowArray(m) {
   document.getElementById("usednum").innerHTML += (m + ', ');
}


</script>
<input type="button" style="font-size:9pt" value="Gen Random" id="BtnCall">
</input>
</form>
</body>
</html>
&#13;
&#13;
&#13;