Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have array

var arr = ['elem1', 'elem2', 'elem3', 'elem4', 'elem5', 'elem6', 'elem7', 'elem8'];

How could I echo 4 random elements of it using javascript?

share|improve this question

marked as duplicate by mplungjan, Tibos, Sirko, Louis, TimWolla Mar 15 '14 at 3:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Unique or non-unique random elements? –  MackieeE Nov 12 '13 at 9:05
2  
possible duplicate of How to get random elements from an array which is a duplicate of Getting random value from an array - please pay attention to the suggestions you get when you ask –  mplungjan Nov 12 '13 at 9:05
    
Here what I need: arr.sort( function() { return 0.5 - Math.random() } ); for (var i=0;i<4;i++) { document.write(arr[i]); } –  user2963041 Nov 12 '13 at 9:11
    
another techniques Array.apply( null , new Array(4) ).map( function( v ){ return arr[Math.floor( Math.random( ) * arr.length ) ];}); ` –  rab Nov 12 '13 at 9:24

6 Answers 6

You can use the below code to get a random element:

arr[Math.floor(Math.random() * arr.length)];
share|improve this answer

What about this:

var arr = ['elem1', 'elem2', 'elem3', 'elem4', 'elem5', 'elem6', 'elem7', 'elem8'];

arrLength = arr.length;

var randomNummer = Math.floor(Math.random()*arrLength);

alert(arr[randomNummer]);

jsFiddle

share|improve this answer

Use inbuilt Math.Random:

for (var i=0;i<4;i++)
{ 
  var rnd = Math.floor((Math.random()*arr.length);
  console.log(arr[rnd]);
}
share|improve this answer

If you want distinct elements, you can extract them one at a time from the array

function extractRandomElement(arr) {
  var index = Math.floor(Math.random() * arr.length);
  return arr.splice(index, 1)[0];
}

var arr = ['elem1', 'elem2', 'elem3', 'elem4', 'elem5', 'elem6', 'elem7', 'elem8'];
var arrCopy = arr.slice(0); // copy the array so the original is unchanged
var result = [];
var N = 4;

for (var i=0; i<N; i++) {
  result.push(extractRandomElement(arrCopy));
}

console.log(result);

An alternative is to shuffle (sort randomly) the array then get the first 4 elements:

var arr = ['elem1', 'elem2', 'elem3', 'elem4', 'elem5', 'elem6', 'elem7', 'elem8'];
var arrCopy = arr.slice(0); // copy the array so the original is unchanged
var N = 4;

var result = arrCopy.sort(function(){ return Math.random()-0.5; }).slice(N);

console.log(result);

This answer from a question that duplicates this one is very good: http://stackoverflow.com/a/7159251/1669279

It is an improvement over the first method above:

function extractRandomElement(arr) {
  var index = Math.floor(Math.random() * arr.length);
  var retVal = arr[index];
  arr[index] = arr.pop();
  return retVal;
}
share|improve this answer

If you are trying to get 4 unique elements of that array, you should always remove the element that you randomly retrieved:

var arr = ['elem1', 'elem2', 'elem3', 'elem4', 'elem5', 'elem6', 'elem7', 'elem8'],
    random_values = [];

for (var i = 0; i < 4; i++ ) {
    var length = arr.length,
        random = Math.floor(Math.random()*length+1);

    random_values.push(arr[random]);

    // Remove the already selected element from the arr array
    arr.splice(random, 1);
}

console.log(random_values);
share|improve this answer

You can use the below code to get a 4 random element:

var element1 = arr[Math.floor(Math.random()  * arr.length)];
var element2 = arr[Math.floor(Math.random()  * arr.length)];
var element3 = arr[Math.floor(Math.random()  * arr.length)];
var element4 = arr[Math.floor(Math.random()  * arr.length)];
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.