-1

I have this code that displays 100 random numbers, then adds a break for every 10 numbers. How would I ensure that these 100 random numbers do not have repeats? I know maybe there's something to do with more if statements?

Thanks, J

<!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>
  • 1
    you may have a look into shuffling: https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array – Nina Scholz Dec 05 '17 at 21:32
  • 2
    Tip: `if( i % 10 === 9 )` does the same thing as those 10 separate if statements. – JJJ Dec 05 '17 at 21:33
  • 1
    Possible duplicate of [Generate unique random numbers between 1 and 100](https://stackoverflow.com/questions/2380019/generate-unique-random-numbers-between-1-and-100) – JJJ Dec 05 '17 at 21:34
  • Thanks for the tip, that saved a lot of useless codes. But that possibly duplicate still doesn't tell me how I can prevent these 100 numbers from being the same? –  Dec 05 '17 at 21:42
  • 1
    If you're looking for 100 non-repeating numbers that go from 1-100 just shuffle. Otherwise you'll need to track used numbers to see if you have a dupe. – Dave Newton Dec 05 '17 at 21:43

3 Answers3

1

How would I ensure that these 100 random numbers do not have repeats?

You need to save returned values (by random number generator) so that same value is not returned again.

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;
   }
}

Now you can call this n number of times ( n < (max - min) )

var nRandomNumbers = 10;
var randomNumArr = [];
var ran1 = randomNumberGenerator(1,100); //initialize the randomNumberGenerator
for( var counter = 0; counter < nRandomNumbers; counter++ )
{
  randomNumArr.push( ran1() );
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • For 100 numbers from 1-100 just shuffle. – Dave Newton Dec 05 '17 at 21:44
  • @DaveNewton If OP is looking to fetch 100 numbers from 1 to 100, then I agree with you. However, OP has mentioned *How would I ensure that these 100 random numbers do not have repeats?* OP didn't said that those 100 necessarily have to be *n* to *n + 100*. – gurvinder372 Dec 05 '17 at 21:48
  • 100 numbers from 1-100 was just an example, I would use this if I needed 100 numbers from say 1-500 right? –  Dec 05 '17 at 21:53
  • You can also use this if you want to get 100 from 1-100, though this wouldn't be the best approach for that requirement - **shuffling** would be. – gurvinder372 Dec 05 '17 at 21:54
1

You can use Fischer yates algorithm for shuffling an array and shuffle an array from 1 to 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;
Nikhil Ranjan
  • 994
  • 12
  • 16
0

I think this is what you want, if you don't know what something does, please research it. Do not simply copy and paste.

      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>
Eli Richardson
  • 934
  • 7
  • 25