Skip to content
DeveloperMemos

Getting a Random Array Item in JavaScript

JavaScript, Array, Random2 min read

When working with arrays in JavaScript, you may encounter situations where you need to retrieve a random item from an array. Whether you're building a game, implementing a feature that requires randomness, or simply want to shuffle the elements, obtaining a random array item is a common task. In this article, we will explore various approaches to accomplish this in JavaScript. Let's dive in!

Method 1: Using Math.random()

One straightforward way to get a random item from an array is by utilizing the Math.random() function in JavaScript. This method involves generating a random index within the range of the array length and retrieving the element at that index.

Here's an example code snippet:

1function getRandomItem(array) {
2 const randomIndex = Math.floor(Math.random() * array.length);
3 return array[randomIndex];
4}
5
6const myArray = [1, 2, 3, 4, 5];
7const randomItem = getRandomItem(myArray);
8console.log(randomItem);

In this code, the getRandomItem function accepts an array as a parameter. It generates a random index using Math.random() multiplied by the length of the array. By flooring the result, we ensure we get a valid index within the range of the array. Finally, the function returns the element at the randomly generated index.

Method 2: Using the Fisher-Yates Algorithm

Another popular approach to obtain a random item from an array is by using the Fisher-Yates algorithm. This algorithm shuffles the elements of the array in place, and then you can simply retrieve the first element to achieve randomness.

Here's an example of how to implement the Fisher-Yates algorithm in JavaScript:

1function getRandomItem(array) {
2 let currentIndex = array.length;
3 let temporaryValue, randomIndex;
4
5 while (currentIndex !== 0) {
6 randomIndex = Math.floor(Math.random() * currentIndex);
7 currentIndex -= 1;
8
9 temporaryValue = array[currentIndex];
10 array[currentIndex] = array[randomIndex];
11 array[randomIndex] = temporaryValue;
12 }
13
14 return array[0];
15}
16
17const myArray = [1, 2, 3, 4, 5];
18const randomItem = getRandomItem(myArray);
19console.log(randomItem);

In this example, we iterate over the array from the last element to the first (index 0). Within each iteration, we generate a random index and swap the element at the current index with the one at the random index. Finally, we return the element at the first index of the shuffled array.

Method 3: Using the Lodash Library

If you're already using the popular Lodash library in your JavaScript project, you can leverage its _.sample() function to achieve the same result with a more concise syntax.

Here's an example of how to use _.sample() to obtain a random item from an array:

1const _ = require('lodash');
2
3const myArray = [1, 2, 3, 4, 5];
4const randomItem = _.sample(myArray);
5console.log(randomItem);

By importing the Lodash library, you gain access to the _.sample() function, which returns a random element from the provided array.

Wrap Up

In this article, we explored different methods to obtain a random item from an array in JavaScript. You can choose the approach that suits your needs based on the complexity of your project and any additional dependencies you may have. Whether you prefer the simplicity of Math.random(), the effectiveness of the Fisher-Yates algorithm, or the convenience of the Lodash library, now you have the tools to retrieve random array items with ease.