This makes the values of the array not repeat any previous position for n shuffles (i'm using half of the array's size as n, after that i restart the forbidden indexes). At the end a modified this version to make it not repeat the current position.
For this you will have to save a history of all the index where each value of the orignal array has been. To do this i've added a little more complexity to your numbers
var numberArray = [{value:1, unavailable_indexes:[0]},
{value:2, unavailable_indexes:[1]},
{value:3, unavailable_indexes:[2]},
{value:4, unavailable_indexes:[3]},
{value:5, unavailable_indexes:[4]},
{value:6, unavailable_indexes:[5]},
{value:7, unavailable_indexes:[6]},
{value:8, unavailable_indexes:[7]},
{value:9, unavailable_indexes:[8]}
];
this way you have the number in value and an array of all the positions where it has been. Next we need to run all the array and switch numbers around.
var arrayLen = numberArray.length-1;
$.each(numberArray, function(index, value){
var newIndex;
//restart the array when half of the index have been covered or it will take awhile to get a random index that wasn't used
if(value.unavailable_indexes.length >= numberArray.length/2)
value.unavailable_indexes = [index];//restart the unavailable indexes with the current index as unavailable
do{
newIndex = Math.floor(Math.random()*arrayLen);
//verify if you can swap the 2 values, if any of them have been on the destination index get another random index
}while($.inArray(value.unavailable_indexes, newIndex) || $.inArray(numberArray[newIndex].unavailable_indexes, index));
numberArray[index] = numberArray[newIndex];
numberArray[newIndex] = value;
})
after all the array has been moved around you need to save the positions where they landed
$.each(numberArray, function(index, value){
value.unavailable_indexes.push(index);
}
EDIT: if you just want to prevent it from just repeating the previous position then make unavailable_indexes
hold the last position it was in and replace the do{...}while()
with:
do{
newIndex = Math.floor(Math.random()*arrayLen);
}while(newIndex != value.unavailable_indexes)
and the last method would look like:
$.each(numberArray, function(index, value){
value.unavailable_indexes = index;
}