如何在javascript中重复math.random的结果?

时间:2017-07-09 10:40:32

标签: javascript

只是为了澄清我的问题,我需要分别生成一个2位数字,使它像6这样设置为" 12,45,24,63,64,1"

<html>
 <head>
    <title>lotto picker</title>

 </head>
 <body>


    <p> I can generate you a lotto numbers to bet right now!</p>
    <button id="generateButton">Generate!</button>

 <script type="text/javascript">
   document.getElementById("generateButton").onclick = function() {

                var msg = " ";
                var lottoNum = Math.floor((Math.random() * 64) + 1);


                alert(lottoNum);


            }
 </script>
 </body>


</html>

2 个答案:

答案 0 :(得分:1)

此处1089是所需的范围。

其中:

10是起始编号

89是可能的结果数(10 + start (89) - end (10))

    document.getElementById("generateButton").onclick = function() {
                var msg = " ";                
                for(var i = 0 ;i < 6 ; ++i) {
                var lottoNum = Math.floor(Math.random() *89) + 10;
                msg += lottoNum + ", ";
                }
                msg = msg.slice(0, -1);
                alert(msg);
                }
<html>
<head>
    <title>lotto picker</title>
</head>

<body>
    <p> I can generate you a lotto numbers to bet right now!</p>
    <button id="generateButton">Generate!</button>
</body>

答案 1 :(得分:1)

要生成6个2位数字,您需要循环6次。以下是生成6个2位数字的程序

document.getElementById("generateButton").onclick = function() {
                var msg = " ";
                for(var i = 0 ;i < 6 ; ++i) {
                var lottoNum = Math.floor(Math.random() * 89) + 10;
                msg += lottoNum + ",";
                }
                msg = msg.slice(0, -1);
                alert(msg);
            }
<html>
<head>
    <title>lotto picker</title>
</head>

<body>
    <p> I can generate you a lotto numbers to bet right now!</p>
    <button id="generateButton">Generate!</button>
</body>