0

假设我在 javascript 中有一个数组,例如

[[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]

我可以随机打乱子数组的元素以获得这样的东西吗

[[16,4,10],[8,3,9],[1,14,18],[2,5,7],[6,17,11],[12,13,15]]

编辑:所有子数组的长度相同。并且新数组将具有与旧数组相同长度的子数组。

4

5 回答 5

2

我会为此使用下划线:http: //underscorejs.org/

v = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]
v2 = _.shuffle(_.flatten(v))
v3 = _.groupBy(v2, function(item, i) {
  return i % v.length;
})

于 2013-02-22T21:20:51.280 回答
1

您可以展平您的阵列,将其洗牌,然后再次拆分:

var flat = [].concat.apply([], myArray);
arrayShuffle(flat);
var newArray = [],
    sublen = myArray[0].length;
for (var i=0; i<flat.length; i+= sublen)
    newArray.push(flat.slice(i, i+sublen));

或者您修改现有的洗牌算法之一以使用子数组中的项目。例如,Fisher-Yates-shuffle:

function shuffle2dArray(array) {
    if (!array.length) return array;
    var sublen = array[0].length,
        len = array.length * sublen;
    for (var i = len - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var ix = ~~(i/sublen),
            iy = i % sublen,
            jx = ~~(j/sublen),
            jy = j % sublen;
        var temp = array[ix][iy];
        array[ix][iy] = array[jx][jy];
        array[jx][jy] = temp;
    }
    return array;
}
于 2013-02-22T21:18:39.163 回答
1

这会随机播放,它不会返回新数组:

function shuffleMatrix (m) {
   if (m.length == 0 || m[0].length == 0) {
       // no rows or columns, just return it
       return m;
   }
   function swap(i1, j1, i2, j2) {
       if (i1 != i2 || j1 != j2) {
           var temp = m[i1][j1];
           m[i1][j1] = m[i2][j2];
           m[i2][j2] = temp;
   }
   var rows = m.length;
   var cols = m[0].length;
   for (i = 0; i < rows; i++) {
       for (j = 0; j < cols; j++) {
           var new_i = Math.random()*rows;
           var new_j = Math.random()*cols;
           swap(i, j, new_i, new_j);
       }
   }
}
于 2013-02-22T21:19:27.883 回答
1
function arrayShuffle(){
  var tmp, rand;
  for(var i =0; i < this.length; i++){
    rand = Math.floor(Math.random() * this.length);
    tmp = this[i]; 
    this[i] = this[rand]; 
    this[rand] =tmp;
  }
}

Array.prototype.shuffle =arrayShuffle;

And then

for(var i in arrays){
    arrays[i].shuffle()
}

same thing to position of arrays inside the toplevel array.

//EDIT: Missreading :)

function shuffleAnyNumber (arrays) {
   var numbers = new Array();     //collection of all numbers

   for(var i in this){
       for(var j in arrays[i]){
          numbers.push( arrays[i][j]);        //collect numbers out of the given arrays
       }       
   }

   numbers.shuffle(); //Method shown above

   var output = new Array();
   var tempArray= new Array();

   //putting it together
   for(var i in numbers){
       if(tempArray.length == 3){
            output.push(tempArray);
            tempArray = new Array();
       } else {
            tempArray.push(numbers[i]);
       }
   }

   return output;
}

i ' d say it will work like that.

于 2013-02-22T21:10:10.490 回答
0

我 100% 支持 Ilan Berci 的回答。但是,您可以使用下划线以更实用的样式编写它:

var v = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]];

return _(v).chain().flatten().shuffle().groupBy(function(item, i) { 
    return i % v.length; 
}).values().value();

去下划线!!

于 2013-02-23T00:39:12.593 回答