0

I'm new to javascript and trying to get comfy with Functions, For loops, and If statements. I'm working on a simple exercise that generates 5 random numbers from a function call. One bit of logic that I'm struggling with putting together is comparing the numbers created by Math.random that are pushed into an array. What's the best method to use to ensure that all numbers to push to the array are unique (no duplicates)? Would I add an If statement in the For codeblock that checks every number, and if they match, rerun the Math.random function? Trying to figure out if that's the best way to approach the problem.

function randoNumbers(min, max){

let randomNumbers = [];

for (let counter = 0; counter < 5 ; counter++){
    randomNumbers.push(Math.floor(Math.random() * (max - min) + +min));
    }
    console.log(randomNumbers);

}

randoNumbers(1, 10);
RomeNYRR
  • 877
  • 4
  • 19
  • 34
  • 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) – Robby Cornelissen Oct 23 '18 at 03:30
  • Potential duplicate of https://stackoverflow.com/questions/2380019/generate-unique-random-numbers-between-1-and-100 – Gaurav Suman Oct 23 '18 at 03:39
  • https://gist.github.com/guilhermepontes/17ae0cc71fa2b13ea8c20c94c5c35dc4 – sumit Oct 23 '18 at 03:51

2 Answers2

2

A very plain solution would be to generate numbers until the generated number is not already included in the array, and then push it to the result array:

function randoNumbers(min, max) {
  const randomNumbers = [];
  for (let counter = 0; counter < 5; counter++) {
    let num;
    do {
      num = Math.floor(Math.random() * (max - min) + min);
    }
    while (randomNumbers.includes(num))
    randomNumbers.push(num);
  }
  console.log(randomNumbers);
}

randoNumbers(1, 10);

For slightly better complexity, you could use a Set instead (set.has is quicker than arr.includes):

function randoNumbers(min, max) {
  const set = new Set();
  for (let counter = 0; counter < 5; counter++) {
    let num;
    do {
      num = Math.floor(Math.random() * (max - min) + min);
    }
    while (set.has(num))
    set.add(num);
  }
  console.log([...set]);
}

randoNumbers(1, 10);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • This is great thank you! So overwhelmed with the fact that there are so many ways to solve this example. What's the best method for selecting the right approach, simplicity or complexity? I'll check out the `includes` function to better understand how the solution works – RomeNYRR Oct 23 '18 at 04:27
  • 1
    It depends on the situation, but ceteris paribus, simple is better, because code readability is one of the most important things to strive for. But, for example, if performance is a significant factor (which it rarely is), more complex code that runs more quickly can be worth it. (make sure to comment liberally if what the code is doing isn't obvious at a glance) – CertainPerformance Oct 23 '18 at 04:31
  • Although, here I would argue that using a `Set` doesn't make the code any less readable and I would prefer it over using an array. – slider Oct 23 '18 at 04:52
  • @slider Yep, it's not more complicated at all. – CertainPerformance Oct 23 '18 at 04:53
2

There are many ways to solve this problem. I am contributing my way of solving it.

function randoNumbers(min, max) {

  let randomNumbers = [];

  for (; randomNumbers.length < 5;) {
    const value = Math.floor(Math.random() * (max - min) + +min);
    if (!randomNumbers.includes(value))
      randomNumbers.push(value);
  }
  console.log(randomNumbers);

}



randoNumbers(1, 10);
vijay krishna
  • 514
  • 4
  • 14