0

I have array

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

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

  • Unique or non-unique random elements? – MackieeE Nov 12 '13 at 09:05
  • 2
    possible duplicate of [How to get random elements from an array](http://stackoverflow.com/questions/7158654/how-to-get-random-elements-from-an-array) which is a duplicate of [Getting random value from an array](http://stackoverflow.com/questions/4550505/getting-random-value-from-an-array) - please pay attention to the suggestions you get when you ask – mplungjan Nov 12 '13 at 09: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 09: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 09:24

5 Answers5

1

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: https://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;
}
Community
  • 1
  • 1
Tibos
  • 27,507
  • 4
  • 50
  • 64
0

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

arr[Math.floor(Math.random() * arr.length)];
Ramesh
  • 4,223
  • 2
  • 16
  • 24
0

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

nkmol
  • 8,025
  • 3
  • 30
  • 51
0

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);
vladzam
  • 5,462
  • 6
  • 30
  • 36
0

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)];
Haji
  • 1,999
  • 1
  • 15
  • 21