How to Select a Random Element From an Array in JavaScript
- Select a Random Value From an Array in JavaScript
-
Use the
Math.random()
,array.length
, andMath.floor()
to Select a Random Element From an Array in JavaScript -
Use the
.sample()
Method ofLodash
andUnderscore.js
to Select a Random Element From an Array in JavaScript -
Use the Bitwise Operators
NOT
(~~
) andOR
(|
) 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
, andMath.floor()
together.- Use
.sample()
Method ofLodash
andUnderscore.js
. - Use bitwise operators
NOT
andOR
.
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
Related Article - JavaScript Array
- How to Check if Array Contains Value in JavaScript
- How to Create Array of Specific Length in JavaScript
- How to Convert Array to String in JavaScript
- How to Remove First Element From an Array in JavaScript
- How to Search Objects From an Array in JavaScript
- How to Convert Arguments to an Array in JavaScript