使用random()时如何确保没有重复的数字?

时间:2017-12-05 21:30:58

标签: javascript html for-loop numbers repeat

我有这个代码显示100个随机数,然后每10个数字添加一个中断。我如何确保这100个随机数没有重复?我知道可能还有更多if语句吗?

谢谢, Ĵ



<!DOCTYPE html>
<html>
  <h1><font color = "blue">Random Numbers and Breaks (for loops)</font></h1> 
  <p><font color="red">This will display 100 numbers and insert a break in 10-number intervals:</font></p>
  <div id="demo"> </div>
  <body>
    <script> 
      var text= '';
      var i = 0;
      var counter = 0;

      for (; i < 100; i++ , counter++) { 
      var numbers = Math.floor((Math.random() * 100) + 1);
      text += numbers + "  " + "<br>";
      if (i % 10 === 9) {
          text += "<br>";
      }
    }        
    document.getElementById("demo").innerHTML = text;

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

3 个答案:

答案 0 :(得分:1)

  

我如何确保这100个随机数没有重复?

您需要保存返回值(通过随机数生成器),以便不再返回相同的值。

function randomNumberGenerator( min, max )
{
   var returnedValues = [];
   return function()
   {
       var num = null;
       while( num == null ||  returnedValues.indexOf( num ) != -1 )
       {
           num  = Math.floor((Math.random() * (max-min)) + min);
       }
       return num;
   }
}

现在你可以调用n(n <(最大 - 最小))

var nRandomNumbers = 10;
var randomNumArr = [];
var ran1 = randomNumberGenerator(1,100); //initialize the randomNumberGenerator
for( var counter = 0; counter < nRandomNumbers; counter++ )
{
  randomNumArr.push( ran1() );
}

答案 1 :(得分:1)

您可以使用Fischer yates算法对数组进行混洗,并将数组从1到100进行混洗。

// Returns num size random array from 1 to num
function getRandomArray(num) {
  const arr = [];

  for (let i = 0; i < num; i++) {
    arr.push(i+1);
  }
  let temp, rIndex;
  // swap random element
  for (i = num - 1; i >= 0; i--) {
    rIndex = Math.floor(Math.random()*i);
    temp = arr[i];
    arr[i] = arr[rIndex];
    arr[rIndex] = temp;
  }

  return arr;
}

let text = '';
getRandomArray(100).forEach(function(item, index){
  text += item + " <br />";
  text += (index + 1)%10 === 0 ? "<br />" : "";
});
document.getElementById("demo").innerHTML = text;

答案 2 :(得分:0)

我认为这就是你想要的,如果你不知道是什么,请研究它。不要简单地复制和粘贴。

      var text= '';
      var i = 0;
      var counter = 0;
      var nums = []; // Created array
      var numbers;
      for (; i < 100; i++ , counter++) { 
        numbers = Math.floor((Math.random() * 100) + 1);
        while (nums.includes(numbers))
          numbers = Math.floor((Math.random() * 100) + 1);
        // While nums array includes numbers, reroll.
        nums.push(numbers); // Push the number to the array
        text += numbers + "  " + "<br>";
        if (i % 10 === 9) text += "<br>"
      // If the rightmost digit is a 9, add the br tag
    }        
    document.getElementById("demo").innerHTML = text;
<!DOCTYPE html>
<html>
  <h1><font color = "blue">Random Numbers and Breaks (for loops)</font></h1> 
  <p><font color="red">This will display 100 numbers and insert a break in 10-number intervals:</font></p>
  <div id="demo"> </div>
  <body>
  </body>
</html>