How to select a random element from array in JavaScript ?

We are going to learn how can we select a random element from an array in JavaScript. we will be given an array and we need to print a random element from that array.

Selecting a random element from an array is a common task, especially in scenarios where you need to introduce randomness or unpredictability into your code. Whether you’re building a game, conducting statistical analysis, or implementing a feature that requires random selection, knowing how to pick a random element from an array is important.

How to select a random element from an array in JavaScript?

These are the following approaches for solving this problem:

Table of Content

  • Using Math.random() function
  • Using custom function
  • Using Lodash _.sample method
  • Using Array.splice() with Math.random()
  • Using Array destructuring and Math.floor()

Using Math.random() function

  • Use the “Math.random()” function to get the random number between(0-1, 1 exclusive).
  • Multiply it by the array length to get the numbers between(0-arrayLength).
  • Use the “Math.floor()” to get the index ranging from(0 to arrayLength-1).

Example: This example implements the above approach.  

Javascript
let arr = ["GFG_1", "w3wiki",
    "Beginner", "Computer Science Portal"];

function GFG_Fun() {
    console.log(arr[(Math.floor(Math.random() * arr.length))]);
}
GFG_Fun()

Output
w3wiki

Using custom function

  • The random(a, b) method is used to generate a number between(a to b, b exclusive).
  • Taking the floor value to range the numbers from (1 to array length).
  • Subtract 1 to get the index ranging from(0 to arrayLength-1).

Example: This example implements the above approach. 

Javascript
let arr = ["GFG_1", "w3wiki",
    "Beginner", "Computer Science Portal"];

function random(mn, mx) {
    return Math.random() * (mx - mn) + mn;
}

function GFG_Fun() {
    console.log(arr[(Math.floor(random(1, 5))) - 1]);
}
GFG_Fun()

Output
w3wiki

Using Lodash _.sample method

  • In this approach we are using the third party librray.
  • The _.sample() method takes an array as an input an returns the random element from that given array.
  • we willuse _.sample() method to print random element from an array.

Example: This example implements the above approach. 

Javascript
// Requiring the lodash library 
const _ = require("lodash");

// Original array 
let array1 = ([3, 6, 9, 12]);

// Use of _.sample() method
let gfg = _.sample(array1);

// Printing original Array 
console.log("original array1: ", array1)

// Printing the output 
console.log(gfg);

Output:

original array1: [ 3, 6, 9, 12 ]
12

Using Array.splice() with Math.random()

In the Array.splice() with Math.random() approach, generate a random index within the array length. Use Array.splice() to remove the element at the random index and return it. This method modifies the original array.

Example: In this example the getRandomElement() function takes an array as input, generates a random index within the array length, removes the element at that index using Array.splice(), and returns the removed element. The original array is modified by removing the selected element.

JavaScript
function getRandomElement(array) {
    const randomIndex = Math.floor(Math.random() * array.length);
    const randomElement = array[randomIndex];
    return randomElement;
}

const array = [1, 2, 3, 4, 5];
const randomElement = getRandomElement(array);
console.log("Random element selected:", randomElement);
console.log("Original array:", array); 

Output
Random element selected: 3
Original array: [ 1, 2, 3, 4, 5 ]

Using Array destructuring and Math.floor()

In this approach, we generate a random index between 0 and array length – 1 using Math.random(). Then, we use array destructuring to directly extract the element at that random index.

Example: This example implements the above approach.

JavaScript
// Function to select a random element from an array
function getRandomElement(array) {
    // Generate a random index
    const randomIndex = Math.floor(Math.random() * array.length);
    // Use array destructuring to directly extract the random element
    const randomElement = array[randomIndex];
    return randomElement;
}

// Example usage
const originalArray = [10, 20, 30, 40, 50];
const randomItem = getRandomElement(originalArray);
console.log(randomItem);

Output
30

Using Array.prototype.reduce Method

Another approach to select a random element from an array in JavaScript is by using the reduce method in combination with Math.random(). This method involves iterating through the array and selecting a random element based on a cumulative probability.

Example: In this example, we will use the reduce method to randomly select an element from an array.

JavaScript
function getRandomElementWithReduce(array) {
    return array.reduce((selected, item) => {
        return Math.random() < 1 / (array.length) ? item : selected;
    }, array[0]);
}

// Example usage
const sampleArray = [100, 200, 300, 400, 500];
const randomItemWithReduce = getRandomElementWithReduce(sampleArray);
console.log(randomItemWithReduce);

Output
100


JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.



Contact Us