I have an scenario to generate random numbers that should generate from the given numbers.
for example, I have an array num=[23,56,12,22]. so i have to get random number from the array
You can make a function that returns a random integer between 0 and the length of the array, like this:
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
Then call it, like this:
let randomInt = getRandonInt(lengthOfArray);
console.log(randomInt);
Expected output: 0, 1, 2 .. length of array
Then just simply use the randomInt to get whatever you need from your array entries.