Say I have a JavaScript array:
var my_arr = [1,2,3,4,5,6];
There are lots of way to shuffle it, say I already got a function shuffle
, like this:
shuffle(my_arr);
// The array might be like [1,5,4,3,2,6];
But when I run it again, the order just changed. I'd like to know whether the shuffle can be fixed to a given value (a number, or a string, or even anything). I need the result to be the same every time if the key is the same key. For example:
var my_arr = [1,2,3,4,5,6];
function shuffle_by_key(arr, key){....}
var new_arr1 = shuffle_by_key(my_arr, 1);
var new_arr2 = shuffle_by_key(my_arr, 2);
var new_arr3 = shuffle_by_key(my_arr, 1);
The new_arr1
should be the same as new_arr3
, because they are generated by the same key "1".