0

I wanna generate a 4-digit number with no duplicates, here are the functions that generate a random number with 4-digits. I'm curious if there is a JS method that can do that for me, or do I have to loop through the elements, or even do a while loop until it generates a number with no duplicates (since it's only 4 digits, it is small anyway)..

function genRandomNumber(range = 10) {
  return Math.floor(Math.random() * range);
}

function HiddenNumber(digit = 4) {
    const number = Array.from(
  {
    length: digit
  }, () => genRandomNumber()).join("");
  console.log(number);
  return Number(number);
}
legenw84it
  • 51
  • 6
  • 1
    Does this answer your question? [Javascript - generate a randome 4 digit number with no repeating digits](https://stackoverflow.com/questions/32104963/javascript-generate-a-randome-4-digit-number-with-no-repeating-digits) Also this video : https://www.youtube.com/watch?v=WUOOMVaZRe4 – flakerimi Aug 23 '21 at 01:31

0 Answers0