jsFiddle Demo
You will need to align a random value for each value in the array, as far as I can tell. In this regard, you probably want to do something like this:
for( var i = 0; i < length; i++ ){ seed.push(Math.random()); }
Where do you guarantee that length is the same length as for the seed. This will be 4 for your simple example. Once this is done, you can pass the seed to your function (or sort) randomly to ensure that you get the same results. Shuffle will need to use it also in the loop
var randomIndex = parseInt(seed[i] * (len - i));
So, this is what it all breaks down into
The seed function that will store the array of seeds
var seeder = function(){ var seed = []; return { set:function(length){ for( var i = 0; i < length; i++ ){ seed.push(Math.random()); } return seed; }, get: function(){ return seed; }, clear: function(){ seed = []; } }; }
Pretty simple shuffle
function randomShuffle(ar,seed){ var numbers = []; for( var a = 0, max = ar.length; a < max; a++){ numbers.push(a); } var shuffled = []; for( var i = 0, len = ar.length; i < len; i++ ){ var r = parseInt(seed[i] * (len - i)); shuffled.push(ar[numbers[r]]); numbers.splice(r,1); } return shuffled; }
Is used
var arr = ["a", "b", "c", "d"]; var seed = seeder(); seed.set(arr.length); console.log(randomShuffle(arr,seed.get())); console.log(randomShuffle(arr,seed.get())); console.log(randomShuffle(arr,seed.get()));
source share