zip = function() {
var args = [].slice.call(arguments, 0);
return args[0].map(function(_, i) {
return args.map(function(a) {
return a[i]
})
})
}
unzip = function(a) {
return a[0].map(function(_, i) {
return a.reduce(function(y, e) {
return y.concat(e[i])
}, [])
})
}
shuffle = function(a) {
for (var i = a.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var t = a[i];
a[i] = a[j];
a[j] = t;
}
return a;
}
z = unzip(shuffle(zip(one, two, three)))
one = z[0]
two = z[1]
three = z[2]
少し冗長ですが、動作します...
この場合、おそらくより高速な別のオプション:
range = function(n) {
for(var r = [], i = 0; i < n; i++)
r.push(i);
return r;
}
pluck = function(a, idx) {
return idx.map(function(i) {
return a[i];
});
}
r = shuffle(range(one.length))
one = pluck(one, r)
two = pluck(two, r)
three = pluck(three, r)
また、3 つの変数の代わりに配列の配列を使用することをお勧めします。
matrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
];
r = shuffle(range(matrix[0].length));
matrix = matrix.map(function(row) {
return pluck(row, r)
});