且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 JavaScript 中生成两个数字之间的随机数

更新时间:2023-02-10 12:09:54

重要

以下代码仅在最小值为1"时才有效.它不适用于除1"以外的最小值.

如果你想得到一个介于 1(并且只有 1)和 6 之间的随机整数,你可以计算:

Important

The following code works only if the minimum value is `1`. It does not work for minimum values other than `1`.

If you wanted to get a random integer between 1 (and only 1) and 6, you would calculate:

    const rndInt = Math.floor(Math.random() * 6) + 1
    console.log(rndInt)

地点:

  • 1 是起始编号
  • 6 是可能的结果数 (1 + start (6) - end (1))
  • 1 is the start number
  • 6 is the number of possible results (1 + start (6) - end (1))