如何在JavaScript中的两个指定变量之间生成随机整数,例如x=4和y=8将输出4、5、6、7、8中的任意一个?


当前回答

加密功能强

要获取[x,y]范围内的加密强随机整数,请尝试:

让cs=(x,y)=>x+(y-x+1)*crypto.getRandomValues(新Uint32Array(1))[0]/2**32|0控制台日志(cs(4,8))

其他回答

下面是一个JavaScript函数的示例,它可以在不使用Math.random()的情况下生成任意指定长度的随机数:

function genRandom(length)
{
  const t1 = new Date().getMilliseconds();
  var min = "1", max = "9";
  var result;
  var numLength = length;
  if (numLength != 0)
  {
     for (var i = 1; i < numLength; i++)
     {
        min = min.toString() + "0";
        max = max.toString() + "9";
     }
  }
  else
  {
     min = 0;
     max = 0;
     return;
  }

  for (var i = min; i <= max; i++)
  {
       // Empty Loop
  }

  const t2 = new Date().getMilliseconds();
  console.log(t2);
  result = ((max - min)*t1)/t2;
  console.log(result);
  return result;
}

使用以下代码,您可以在给定范围内生成一个随机数数组,而无需重复。

function genRandomNumber(how_many_numbers, min, max) {

    // Parameters
    //
    //   how_many_numbers: How many numbers you want to
    //                     generate. For example, it is 5.
    //
    //   min (inclusive):  Minimum/low value of a range. It
    //                     must be any positive integer, but
    //                     less than max. I.e., 4.
    //
    //   max (inclusive):  Maximum value of a range. it must
    //                     be any positive integer. I.e., 50
    //
    //   Return type: array

    var random_number = [];
    for (var i = 0; i < how_many_numbers; i++) {
        var gen_num = parseInt((Math.random() * (max-min+1)) + min);
        do {
            var is_exist = random_number.indexOf(gen_num);
            if (is_exist >= 0) {
                gen_num = parseInt((Math.random() * (max-min+1)) + min);
            }
            else {
                random_number.push(gen_num);
                is_exist = -2;
            }
        }
        while (is_exist > -1);
    }
    document.getElementById('box').innerHTML = random_number;
}

random(min,max)生成介于min(含)和max(不含)之间的随机数Math.floor将数字向下舍入到最接近的整数函数generateRandomInteger(最小,最大){return数学下限(随机(最小值,最大值))}

因此,要生成一个介于4和8之间的随机整数,请使用以下参数调用上述函数:

generateRandomInteger(4, 9)

对于具有范围的随机整数,请尝试:

function random(minimum, maximum) {
  var bool = true;

  while (bool) {
    var number = (Math.floor(Math.random() * maximum + 1) + minimum);
    if (number > 20) {
      bool = true;
    } else {
      bool = false;
    }
  }

  return number;
}

Use:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
    </head>

    <body>
        <script>
            /*
                Assuming that window.crypto.getRandomValues
                is available, the real range would be from
                0 to 1,998 instead of 0 to 2,000.

                See the JavaScript documentation
                for an explanation:

                  https://developer.mozilla.org/en-US/docs/Web/API/RandomSource/getRandomValues
            */
            var array = new Uint8Array(2);
            window.crypto.getRandomValues(array);
            console.log(array[0] + array[1]);
        </script>
    </body>
</html>

Uint8Array创建一个最多三位数的数组,最大值为999。这段代码很短。