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);
3

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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
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)
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 Yep, it's not more complicated at all.
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.