问题
I've got two arrays
var mp3 = ['sing.mp3','song.mp3','tune.mp3','jam.mp3',etc];
var ogg = ['sing.ogg','song.ogg','tune.ogg','jam.ogg',etc];
i need to shuffle both arrays so that they come out the same way, ex:
var mp3 = ['tune.mp3','song.mp3','jam.mp3','sing.mp3',etc];
var ogg = ['tune.ogg','song.ogg','jam.ogg','sing.ogg',etc];
there's a few posts on stackoverflow that shuffle arrays in different ways--this one is pretty great--but none of them demonstrate how to shuffle two arrays in the same exact way.
thnx!
回答1:
Add an extra argument to the Fisher-Yates shuffle. (assumes that your arrays are equal length)
var mp3 = ["sing.mp3", "song.mp3"];
var ogg = ["sing.ogg", "song.ogg"];
function shuffle(obj1, obj2) {
var index = obj1.length;
var rnd, tmp1, tmp2;
while (index) {
rnd = Math.floor(Math.random() * index);
index -= 1;
tmp1 = obj1[index];
tmp2 = obj2[index];
obj1[index] = obj1[rnd];
obj2[index] = obj2[rnd];
obj1[rnd] = tmp1;
obj2[rnd] = tmp2;
}
}
shuffle(mp3, ogg);
console.log(mp3, ogg);
UPDATE:
If you are going to support more arrays (as suggested in the comments), then you could modify the Fisher-Yates as follows (aswell as perform some checks to make sure that the arguments are of Array and that their lengths match).
var isArray = Array.isArray || function(value) {
return {}.toString.call(value) !== "[object Array]"
};
var mp3 = ["sing.mp3", "song.mp3", "tune.mp3", "jam.mp3"];
var ogg = ["sing.ogg", "song.ogg", "tune.ogg", "jam.ogg"];
var acc = ["sing.acc", "song.acc", "tune.acc", "jam.acc"];
var flc = ["sing.flc", "song.flc", "tune.flc", "jam.flc"];
function shuffle() {
var arrLength = 0;
var argsLength = arguments.length;
var rnd, tmp;
for (var index = 0; index < argsLength; index += 1) {
if (!isArray(arguments[index])) {
throw new TypeError("Argument is not an array.");
}
if (index === 0) {
arrLength = arguments[0].length;
}
if (arrLength !== arguments[index].length) {
throw new RangeError("Array lengths do not match.");
}
}
while (arrLength) {
rnd = Math.floor(Math.random() * arrLength);
arrLength -= 1;
for (argsIndex = 0; argsIndex < argsLength; argsIndex += 1) {
tmp = arguments[argsIndex][arrLength];
arguments[argsIndex][arrLength] = arguments[argsIndex][rnd];
arguments[argsIndex][rnd] = tmp;
}
}
}
shuffle(mp3, ogg, acc, flc);
console.log(mp3, ogg, acc, flc);
回答2:
From that example, simply add a second parameter (your second array) and perform the operation on both arrays. You will just need to add and use a second temp, so you aren't overwriting your temps.
This should do the trick ASSUMING THE ARRAYS ARE THE SAME LENGTH:
function shuffle(array, array2) {
var counter = array.length, temp, temp2, index;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
temp = array[counter];
temp2 = array2[counter];
array[counter] = array[index];
array2[counter] = array2[index];
array[index] = temp;
array2[index] = temp2;
}
}
回答3:
I would seriously consider restructuring the way you're keeping track of the information, but in general you can separate out the shuffle itself from the stuff being shuffled. You need a function to generate a random permutation, and then a function to apply a permutation to an array.
function shuffle(o) { //v1.0
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
function permutation( length ) {
var p = [], i;
for (i = 0; i < length; ++i) p[i] = i;
return shuffle(p);
}
function permute( a, p ) {
var r = [];
for (var i = 0; i < a.length; ++i)
r.push(a[p[i]]);
for (i = 0; i < a.length; ++i)
a[i] = r[i];
}
Then you can create a single random permutation and apply it to any list (of the right length) you want.
var p = permutation( mp3.length );
permute(mp3, p);
permute(ogg, p);
permute(aac, p);
// etc
(Shuffle function taken from the SO question linked in the OP.)
回答4:
If you have two arrays of length 2, @Xotic750s function always returns the same value. UseMath.round instead of Math.floor.
function shuffle_two_arrays_identically(arr1, arr2){
"use strict";
var l = arr1.length,
i = 0,
rnd,
tmp1,
tmp2;
while (i < l) {
rnd = Math.round(Math.random() * i)
tmp1 = arr1[i]
tmp2 = arr2[i]
arr1[i] = arr1[rnd]
arr2[i] = arr2[rnd]
arr1[rnd] = tmp1
arr2[rnd] = tmp2
i += 1
}}
回答5:
<script>
var arrayList= ['a','b','c','d','e','f','g'];
arrayList.sort(function(){
return 0.5 - Math.random()
})
document.getElementById("output").innerHTML = arrayList;
<script>
来源:https://stackoverflow.com/questions/18194745/shuffle-multiple-javascript-arrays-in-the-same-way