0

This is the array:
array = [1, 6, 9, 10 , 15, 18, 20];

And a random number is being generated from 1 to 20, with the following code:
var x = Math.floor((Math.random() * 20) + 1)

How could I check if the generated number is already in the array above? If it is, I want to generate a new value so it does not repeat one of the values in the array.

zcoop98
  • 2,590
  • 1
  • 18
  • 31
  • 1
    `array.includes(x)` https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/includes – Shinigami Jul 14 '20 at 10:57
  • Show what you've tried so far – Nick McCurdy Jul 14 '20 at 10:58
  • 4
    Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – Rohit Ambre Jul 14 '20 at 11:00
  • 1
    Array.includes(x) works just fine, thanks ! – Pricop George-Tiberiu Jul 14 '20 at 11:00
  • Just so you know, you are re-inventing the wheel for the "pick a card from a deck of cards" problem. The common solution for a problem where you have a limited amount of things you can pick, is just to just shuffle the deck, and pull cards from it. Getting the last few numbers takes much longer than getting the first few, and trying to get numbers after all your "cards" are picked will never stop. – Sumurai8 Jul 14 '20 at 11:25

6 Answers6

1

You can use array.includes(x)

var number;

do
{
    number = Math.floor((Math.random() * 20) + 1) )
} while (array.includes(number))

Just be aware of unending loop, if you fill the array.

Neophear
  • 492
  • 6
  • 16
1

You can use indexOf() to check if the value is in array

and after that you can use comparisson to generate new number if the random number is the same as the previous

array = [1, 6, 9, 10 , 15, 18, 20]

var x = Math.floor((Math.random() * 20) + 1) )

if (array.indexOf(x) !== -1) {
    //value exists in array
    var x_old=x;

    x = Math.floor((Math.random() * 20) + 1) )

    while (x_old === x) {
       x = Math.floor((Math.random() * 20) + 1) )
    } 

    
} else {
    //Value doesn't exist in array
}

Rui Leming
  • 61
  • 10
1

You can use Set Api for this too The Set object lets you store unique values of any type, whether primitive values or object references. read more here

in your case you can do something like this

let numbers = new Set(); // your collection
const generateNumber = () => Math.floor((Math.random() * 20) + 1)

// add new number to the set
function addNumber(value) {
  numbers.add(value)
}


// generate random numbers
addNumber(generateNumber()) 
addNumber(generateNumber())
addNumber(generateNumber())


Nakazaki
  • 96
  • 8
0

Check whether the number is already in the array, if so generate a new one.

let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const generateRandomNumber = () => Math.floor((Math.random() * 20) + 1)

let temp = generateRandomNumber();

// console.log(temp)

while (a.includes(temp))
  temp = generateRandomNumber()

a.push(temp);

console.log(a)
Stark Buttowski
  • 1,799
  • 2
  • 10
  • 21
0
do {
    var x = Math.floor((Math.random() * 20) + 1);
} while (array.indexOf(x) === -1)

This code should do it, but it will compute unneccesary things. So I prefer to choose just a random item from the array like this.

var x = array[Math.floor(Math.random() * array.length)];

A lot of people are talking about the include function. I don't prefer to use that because it is a modern function that is very lately supported.

Migats21
  • 176
  • 11
0

You can also do this using the below recursive function. The below solution never logs the generated random number which is present in the array, instead it generates random number recursively until, the number not in the array is found!

var array1 =  [1, 6, 9, 10 , 15, 18, 20];

var x = Math.floor((Math.random() * 20) + 1);

var xIncludesArray1 = array1.includes(x);


function arrFinder() {
  if(!xIncludesArray1) { console.log(x)}
  else { x = Math.floor((Math.random() * 20) + 1);
          xIncludesArray1 = array1.includes(x);
          arrFinder();}

};

arrFinder();


  
Santa
  • 367
  • 2
  • 8