How to Select a Random Element From an Array in JavaScript

Mehvish Ashiq Feb 02, 2024
  1. Select a Random Value From an Array in JavaScript
  2. Use the Math.random(), array.length, and Math.floor() to Select a Random Element From an Array in JavaScript
  3. Use the .sample() Method of Lodash and Underscore.js to Select a Random Element From an Array in JavaScript
  4. Use the Bitwise Operators NOT (~~) and OR (|) to Select a Random Element From an Array in JavaScript
How to Select a Random Element From an Array in JavaScript

This article educates on selecting a random element from an array in JavaScript. It also highlights the use of bitwise operators NOT (~~) and OR (|), useful for small size arrays.

Select a Random Value From an Array in JavaScript

We can use the following ways to select a random element from an array in JavaScript:

  • Math.random(), array.length, and Math.floor() together.
  • Use .sample() Method of Lodash and Underscore.js.
  • Use bitwise operators NOT and OR.

Use the Math.random(), array.length, and Math.floor() to Select a Random Element From an Array in JavaScript

var arrStr = ['Mehvish', 'Tahir', 'John', 'Sania', 'Thomas'];
var randElement = arrStr[Math.floor(Math.random() * arrStr.length)];
console.log(randElement);

Output:

"John"

In the example above, the Math.random() method is used to get a random number between 0 and 1 where 1 is exclusive and 0 is inclusive.

Then, it is multiplied by the array’s size to get the answers between 0 and array.length.

Finally, we use Math.floor() to get the index in between 0 and array.length-1.

var arrInt = [1, 3, 5, 7, 2, 9, 0];
var randElement = arrInt[Math.floor(Math.random() * arrInt.length)];
console.log(randElement);

Output:

9

Use the .sample() Method of Lodash and Underscore.js to Select a Random Element From an Array in JavaScript

let _ = require('lodash');

var arrStr = ['Mehvish', 'Tahir', 'John', 'Sania', 'Thomas'];
var randElement = _.sample(arrStr);
console.log(randElement);

Output:

"Sania"

Here, we are using the .sample() method of lodash library that works on the top of another library named Underscore.js.

This method takes a single parameter, a collection and outputs a random element from that collection.

let _ = require('lodash');

var arrInt = [2, 5, 4, 7, 9, 0, 7];
var randElement = _.sample(arrInt);
console.log(randElement);

Output:

2

We can also use the .sample() method of the Underscore.js library. The difference is that it takes two parameters: the list and the second is the number.

It tells how many random elements you want at a time.

var arrInt = [2, 5, 4, 7, 9, 0, 7];
var randElement = _.sample(arrInt);
console.log(randElement);

Output:

7
var arrInt = [2, 5, 4, 7, 9, 0, 7];
var randElement = _.sample(arrInt, 2);
console.log(randElement);

Output:

[2,9]

Don’t forget to import Underscore.js before using it. You can find more information in detail here.

Use the Bitwise Operators NOT (~~) and OR (|) to Select a Random Element From an Array in JavaScript

var arrStr = ['Mehvish', 'Tahir', 'John', 'Sania', 'Thomas'];
var randElement = arrStr[~~(Math.random() * arrStr.length)];
console.log(randElement)

Output:

"Tahir"

The example above uses an alternative of the Math.floor() method, a bitwise NOT (~~) operator.

However, it is faster but useful for small-size arrays only. We can’t use it when we have millions of elements in an array.

Let’s move ahead with the OR operator with Integer Array. The bitwise OR operator is also faster for small-size arrays.

var arrInt = [2, 4, 6, 7, 3];
var randElement = arrInt[Math.random() * arrInt.length | 0];
console.log(randElement)

Output:

6
Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - JavaScript Array