I have an array with 10 elements and I pick random 6 elements out of these in an other array on window load. I don't want to keep the position of any element in the new array to be same as previous on page refresh. How should I do that?
var tempArray = [1,2,3,4,5,6,7,8,9,10]; //inital array with 10 elements
var newArray = []; //array where random 6 elements will be pushed from tempArray
//randomize function
function randomNoRepeats(array) {
var copy = array.slice(0);
return function() {
if (copy.length < 1) { copy = array.slice(0); }
var index = Math.floor(Math.random() * copy.length);
var item = copy[index];
copy.splice(index, 1);
return item;
};
};
var chooser = randomNoRepeats(tempArray);
for(var i=1; i<7; i++){
newArray.push(chooser());
}
newArray = [4,7,9,2,10,1] //Suppose I get this as newArray
newArray = [2,1,5,8,10,3] //On refresh suppose 10 occurs at the same position as previous. Need to avoid this.
Thanks in advance for the help.