Getting a random value from a JavaScript array

0 votes
asked Dec 29, 2010 by sarah


var myArray = ['January', 'February', 'March'];    

How can I select a random value from this array using JavaScript?

14 Answers

0 votes
answered Dec 29, 2010 by jacob-relkin
var rand = myArray[Math.floor(Math.random() * myArray.length)];
0 votes
answered Dec 19, 2011 by markus-amalthea-magn

I've found it even simpler to add a prototype function to the Array class:

Array.prototype.randomElement = function () {
    return this[Math.floor(Math.random() * this.length)]

Now I can get a random array element by just typing:

var myRandomElement = myArray.randomElement()

Note that this will add a property to all arrays, so if you're looping over one using you should use .hasOwnProperty():

for (var prop in myArray) {
    if (myArray.hasOwnProperty(prop)) {

(That may or may not be a hassle for you.)

0 votes
answered Dec 14, 2012 by crazytim

Say you want to choose a random item that is different from the last time (not really random, but still a common requirement)...

Building upon the answer by @Markus, we can add another prototype function:

Array.prototype.randomDiffElement = function(last) {
   if (this.length == 0) {
   } else if (this.length == 1) {
      return this[0];
   } else {
      var num = 0;
      do {
         num = Math.floor(Math.random() * this.length);
      } while (this[num] == last);
      return this[num];

And implement like so:

var myRandomDiffElement = myArray.randomDiffElement(lastRandomElement)
0 votes
answered Dec 13, 2014 by frankies

In my opinion, better than messing around with prototypes , or declaring it just in time, I prefer exposing it to window:

window.choice = function() {
  if (!this.length || this.length == 0) return;
  if (this.length == 1) return this[0];
  return this[Math.floor(Math.random()*this.length)];

Now anywhere on your app you call it like:

var rand =

This way you can still use for(x in array) loop properly

0 votes
answered Dec 28, 2014 by i-g-pascual

If you have fixed values (like a month name list) and want a one-line solution

var result = ['January', 'February', 'March'][Math.floor(Math.random() * 3)]

The second part of the array is an access operation as described in Why does [5,6,8,7][1,2] = 8 in JavaScript?

0 votes
answered Dec 19, 2015 by brendan-nee

If you've already got underscore or lodash included in your project you can use _.sample.

// will return one item randomly from the array
_.sample(['January', 'February', 'March']);

If you need to get more than one item randomly, you can pass that as a second argument in underscore:

// will return two items randomly from the array using underscore
_.sample(['January', 'February', 'March'], 2);

or use the _.sampleSize method in lodash:

// will return two items randomly from the array using lodash
_.sampleSize(['January', 'February', 'March'], 2);
0 votes
answered Dec 24, 2015 by ben-aubin

Prototype Method

If you plan on getting a random value a lot, you might want to define a function for it.

First, put this in your code somewhere:

Array.prototype.sample = function(){
  return this[Math.floor(Math.random()*this.length)];


[1,2,3,4].sample() //=> a random element

Code released into the public domain under the terms of the CC0 1.0 license.

0 votes
answered Dec 2, 2016 by max-heiber

This is similar to, but more general than, @Jacob Relkin's solution:

This is ES2015:

const randomChoice = arr => {
    const randIndex = Math.floor(Math.random() * arr.length);
    return arr[randIndex];

The code works by selecting a random number between 0 and the length of the array, then returning the item at that index.

0 votes
answered Dec 18, 2016 by varun

Here is an example of how to do it:

$scope.ctx.skills = data.result.skills;
    $scope.praiseTextArray = [
    "You\'re ready to move to a new skill", 
    "Yahoo! You completed a problem", 
    "You\'re doing great",  
    "You succeeded", 
    "That was a brave effort trying new problems", 
    "Your brain was working hard",
    "All your hard work is paying off",
    "Very nice job!, Let\'s see what you can do next",
    "Well done",
    "That was excellent work",
    "Awesome job",
    "You must feel good about doing such a great job",
    "Right on",
    "Great thinking",
    "Wonderful work",
    "You were right on top of that one",
    "Beautiful job",
    "Way to go",
    "Sensational effort"

  $scope.praiseTextWord = $scope.praiseTextArray[Math.floor(Math.random()*$scope.praiseTextArray.length)];
0 votes
answered Dec 19, 2016 by foxiris

The shortest version:

var myArray = ['January', 'February', 'March']; 
var rand = myArray[(Math.random() * myArray.length) | 0]
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter