【问题标题】:HTML JavaScript random number selectHTML JavaScript 随机数选择
【发布时间】:2017-12-27 07:56:13
【问题描述】:

我一直在尝试为两个输入之间的数字获取一个随机数生成器。我有代码

<form action="/action_page.php">
<input type="number" name="low" id="low" placeholder="lowest number" value="1"><br>
<input type="number" name="high" id="high" placeholder="highest number" value="10"><br>
<input type="button" value="Submit" onclick="myFunction()">
</form>
<p id="randnum"></p>

<script>
function myFunction() {
    var x = document.getElementById("randnum")
    x.innerHTML = Math.floor((Math.random() * 'low') + 'high');
}
</script>

如您所见,我在一个代码中包含了 javascript。

我输入“低”和“高”的地方是固定数字

【问题讨论】:

    标签: javascript html random numbers


    【解决方案1】:

    您需要获取输入的值并将其应用于计算。 - 请注意,我正在使用 parseInt() 将输入值转换为数字。此外 - 使用 1 作为低值将始终产生 10 的结果,因为你得到了 math.floor - 这将等于 0 - 所以 0 + 10 = 10。

    根据@Quentins 的评论 - 我提醒计算适合您的数字。

    function myFunction() {
        var x = document.getElementById("randnum");
        var high = parseInt(document.getElementById("high").value);
        var low = parseInt(document.getElementById("low").value);
        x.innerHTML = Math.floor(Math.random()*(high-low+1)+low)
    }
    <form action="/action_page.php">
    <input type="number" name="low" id="low" placeholder="lowest number" value="1"><br>
    <input type="number" name="high" id="high" placeholder="highest number" value="10"><br>
    <input type="button" value="Submit" onclick="myFunction()">
    </form>
    <p id="randnum"></p>

    【讨论】:

    • 正确的公式是:Math.floor(Math.random()*(max-min+1)+min);
    • 感谢@Quentin - 修改答案以适合您的计算 - 干杯。
    【解决方案2】:

    <form action="/action_page.php">
    <input type="number" name="low" id="low" placeholder="lowest number" value="1"><br>
    <input type="number" name="high" id="high" placeholder="highest number" value="10"><br>
    <input type="button" value="Submit" onclick="myFunction()">
    </form>
    <p id="randnum"></p>
    
    <script>
    myFunction = () => {
    let min = parseInt(low.value, 0);
    let max = parseInt(high.value, 0);
    
    let x = document.getElementById("randnum")
    x.innerHTML = Math.floor(Math.random() * (max - min)) + min;
    }
    </script>

    【讨论】:

      【解决方案3】:

      我认为这会有所帮助。

      <form action="/action_page.php">
      <input type="number" name="low" id="low" placeholder="lowest number" value="1"><br>
      <input type="number" name="high" id="high" placeholder="highest number" value="10"><br>
      <input type="button" value="Submit" onclick="myFunction()">
      </form>
      <p id="randnum"></p>
      
      <script>
      function myFunction() {
          var x = document.getElementById("randnum")
          var low = document.getElementById("low").value;
          var high = document.getElementById("high").value;
          x.innerHTML = Math.floor((Math.random() * low) + high);
      }
      </script>

      【讨论】:

      • 您的答案是正确的,但是,我必须将“高”更改为“低”,反之亦然才能正常工作。
      • 除了我上次的评论,我好像不能设置最低的数字,只能设置最高的
      【解决方案4】:
      var max = parseInt($("#high")[0].value);
      var min = parseInt($("#low")[0].value);
      x.innerHTML = Math.floor(Math.random() * max) + min;
      

      【讨论】:

      • @MULTITeam 我已经修改了代码。你现在可以试试吗?最简单的公式是 (Math.random() * max) + min
      【解决方案5】:

      首先,在(Math.random() * 'low') + 'high' 中,low 和 high 并不是真正的整数值。

      其次,其他人没有提到的一个非常重要的问题是Math.random() 返回一个介于 0 和 1 之间的双精度值。如果您遵循其他一些答案,随机数将始终为 0 .

      使用这个:

      <form action="/action_page.php">
      <input type="number" name="low" id="low" placeholder="lowest number" 
      value="1"><br>
      <input type="number" name="high" id="high" placeholder="highest number" 
      value="10"><br>
      <input type="button" value="Submit" onclick="myFunction()">
      </form>
      <p id="randnum"></p>
      
      <script>
      function myFunction() {
      var x = document.getElementById("randnum")
      var low = document.getElementById("low").value;
      var high = document.getElementById("high").value;
      var random = Math.random();
      x.innerHTML = Math.floor(random * (high-low) + high);
      }
      </script>
      

      【讨论】:

      • 如果我按照你的建议去做,那就行不通了。但我可以帮你解决这个问题:如果我拿掉一个括号,它会显示 010。如果我从第一个“数学”中替换,它会出现没有整数
      • 感谢您的编辑。您的答案是正确的,但是如何清除随机数末尾的 '0'?
      • @MULTITeam 只是一个错字,我更正了代码。当我发布我的问题时,我发现一些答案并没有解决第二个问题。但就目前而言,Marko Savic 和 gavgrif 都提出了正确的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 2016-05-02
      相关资源
      最近更新 更多