Get Random Element From Array in JavaScript

Get Random Element From Array in JavaScript | We can take the help of the different functions in JavaScript to choose a random element from the given array.

  • Get a random value between 0 to 1 using the Math.random() function, (1 is exclusive). The Math.random() function returns an floating-point value.
  • Use array length to obtain the range of numbers: 0 to array.length.
  • Utilize Math.floor() to obtain the indexes between 0 to array.length-1.

This example uses the strategy described above. You will get different outputs on multiple runs because the getRandomItem() function returns a random element from the given array.

function getRandomItem(arr) {
  // get random index value
  const randomIndex = Math.floor(Math.random() * arr.length);

  // get random item
  const item = arr[randomIndex];

  return item;
}

const array = [1, "Hello", 5, 10];

const result = getRandomItem(array);
console.log(result);

Output:-

Hello

10

Basic knowledge of arrays and using random numbers are all needed to comprehend the technique behind one line of code. We shall begin by addressing the random numbers angle. The following is the formula for selecting a random number from a set of numbers:-

Math.floor(Math.random() * (1 + High - Low)) + Low;

To specify the range of integers we want, we all need a high and a low number. This code will choose a random integer from our range at random (and fairly!). For example, if we want to get a random Integer number between 50 to 100 then we will write:-

// Function to get Integer number between "low" to "high"
// "high" exclusive
function getRandomNumber(low, high) {
  return Math.floor(Math.random() * (1 + high - low)) + low;
}

console.log(getRandomNumber(50, 100));

What are the high and low numbers in our problem, getting a random element from the array? We know that an item’s position within an array depends on an index number. This index number begins at 0, increases by 1, and continues until we reach the last element of our array. For us, the low number will be 0. This is the same as the first element in our array. Right now, the formula will seem as follows:

// since low = 0
Math.floor(Math.random() * (1 + High));

The last item in our array is the high number. By dividing the length of our array by 1, we can determine the index location of the final item in your array:-

let lastItem = myArray.length - 1;

Here is what would result from changing the high variable in our formula to myArray.length – 1:-

Math.floor(Math.random() * (1 + myArray.length - 1));

The 1s will cancel out, giving us the following random index position:-

Math.floor(Math.random() * myArray.length);

There you have it, then. We can write a single line of code that randomly selects a value from an array by simply integrating our understanding of arrays and random integers.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *