var arr1 = [1, 2, 3, 4, 5];
How to suffle this array elements so new array contain this elements in random order without any duplicate element?
E.g: new array = [4, 1, 3, 5, 2];
var arr1 = [1, 2, 3, 4, 5];
How to suffle this array elements so new array contain this elements in random order without any duplicate element?
E.g: new array = [4, 1, 3, 5, 2];
As a person that used jQuery as a "golden hammer" for a long time, my advise would be to use the appropiate tool for each task. In this case, I'd use the shuffle()
method in underscore.js for the task.
var new_array=_.shuffle([1, 2, 3, 4, 5, 6]);
var arr = [1,2,3,4,5,6];
arr = shuffle(arr);
function shuffle(arr) {
for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
return arr;
}
courtesy: http://jsfiddle.net/timur/bAynQ/