0

I have a function that takes user input to tell it how many random numbers to output, and the range to make the random numbers between (say 1-90). The problem is that it will give repeat numbers, but I want only unique numbers. Does anyone know how I can change the code to achieve this?

function random() {
    let randomNums = [];

// Get how many random numbers to output
    let numBox = document.querySelector('.numBox').value;

// Get the highest number range should go to (ex. 1-70)
    let highestNumber = document.querySelector('.highestNumber').value;

// Loop to generate random numbers and push to randomNums array
    for (let i = 0; i < numBox; i++) {
        let num = Math.floor(Math.random() * highestNumber) + 1;
        randomNums.push(` ${num}`)  
    }     

// Sort numbers from lowest to highest
    randomNums.sort(function(a, b) {return a - b;});

// Output numbers
    document.querySelector('.randomOutput').innerHTML = randomNums;
}
fitz2882
  • 1
  • 2

5 Answers5

1

Just change your loop to be a while-loop and check if array doesn't have that value already:

let i = 0;
while(i < numBox) {
    let num = Math.floor(Math.random() * highestNumber) + 1;
    if (randomNums.indexOf(num) == -1) {
        randomNums.push(num);
        i++;
    }
}
Bilal Siddiqui
  • 3,579
  • 1
  • 13
  • 20
0

you can simply generate and verify if it exist until get the necessary random unique numbers

while (randomNums.length < numBox) {
    let num = Math.floor(Math.random() * highestNumber) + 1;
    if (randomNums.indexOf(num) === -1) randomNums.push(num);
}
Kitani Islam
  • 114
  • 6
0

Here's an approach avoiding a while() loop.

  1. Populate an array with the numbers from 1 to the maximum value
  2. Randomise the array (I've borrowed the Fisher-Yates algorithm from this answer)
  3. Pick the first N items from the array

function random() {
  let randomNums = [];

  // Get how many random numbers to output
  let numBox = document.querySelector('.numBox').value;

  // Get the highest number range should go to (ex. 1-70)
  let highestNumber = document.querySelector('.highestNumber').value;

  // Loop to generate random numbers and push to randomNums array
  for (let i = 1; i <= highestNumber; i++) {
    randomNums.push(i)
  }

  randomNums = shuffle(randomNums).slice(0, numBox);

  // Sort numbers from lowest to highest
  randomNums.sort(function(a, b) {
    return a - b;
  });

  // Output numbers
  document.querySelector('.randomOutput').innerHTML = randomNums;
}

function shuffle(a) {
  var j, x, i;
  for (i = a.length - 1; i > 0; i--) {
    j = Math.floor(Math.random() * (i + 1));
    x = a[i];
    a[i] = a[j];
    a[j] = x;
  }
  return a;
}

random();
<input class="numBox" value="4"> out of <input class="highestNumber" value="48">
<div class="randomOutput"></div>
<button onclick="random()">randomize</button>
Constantin Groß
  • 10,719
  • 4
  • 24
  • 50
0

This is the code that fixed it, if anyone else has a similar issue:

function random() {
    let randomNums = [];
    let included = {};
    let numBox = document.querySelector('.numBox').value;
    let highestNumber = document.querySelector('.highestNumber').value;

    for (let i =0; i<numBox; i++) {
        let temp = true;
        while(temp) {
            let num = Math.floor(Math.random() * highestNumber) + 1;
            if (included[num] === undefined) {
                temp = false
                randomNums.push(` ${num}`)
                included[num] = num
               }
        }
    }
    randomNums.sort(function(a, b) {return a - b;});
    document.querySelector('.randomOutput').innerHTML = randomNums;
}
fitz2882
  • 1
  • 2
0

as @CodeManiac mentioned, using "Set" in the @BilalSiddiqui solution.

let randomNums = new Set();
while(randomNums.size < 5) {
    let num = Math.floor(Math.random() * 10) + 1;
        randomNums.add(num);
}
console.log(randomNums)
Akash
  • 848
  • 1
  • 11
  • 30