如何在java中洗牌2d数组
我要做的是洗牌2D数组的值。我有一个2D阵列:如何在java中洗牌2d数组,java,arrays,double,Java,Arrays,Double,我要做的是洗牌2D数组的值。我有一个2D阵列: 2.0|0.0|0.0|1.0|1.0|0.0|0.0|0.0|0.0|1.0|2.0|0.0|1.0|1.0|0.0| 0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0|1.0|0.0|1.0|1.0|0.0|0.0|0.0| 1.0|1.0|1.0|0.0|0.0|1.0|0.0|1.0|0.0|0.0|1.0|0.0|0.0|0.0|0.0| 我想让它洗牌到(eaxmple): 我该怎么做呢?看看集合的源代码。shuffle。
2.0|0.0|0.0|1.0|1.0|0.0|0.0|0.0|0.0|1.0|2.0|0.0|1.0|1.0|0.0|
0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0|1.0|0.0|1.0|1.0|0.0|0.0|0.0|
1.0|1.0|1.0|0.0|0.0|1.0|0.0|1.0|0.0|0.0|1.0|0.0|0.0|0.0|0.0|
我想让它洗牌到(eaxmple):
我该怎么做呢?看看
集合的源代码。shuffle
。它只适用于1D集合,但它为您提供了一种方法:检查所有条目,并使用随机的其他条目交换每个条目
如何使用2D阵列实现这一点?假设它是一个大的1D阵列,用于洗牌。
假设每一行都有相同数量的列(否则会变得稍微复杂),您可以编写此代码,灵感来自集合。shuffle
:
/** Shuffles a 2D array with the same number of columns for each row. */
public static void shuffle(double[][] matrix, int columns, Random rnd) {
int size = matrix.length * columns;
for (int i = size; i > 1; i--)
swap(matrix, columns, i - 1, rnd.nextInt(i));
}
/**
* Swaps two entries in a 2D array, where i and j are 1-dimensional indexes, looking at the
* array from left to right and top to bottom.
*/
public static void swap(double[][] matrix, int columns, int i, int j) {
double tmp = matrix[i / columns][i % columns];
matrix[i / columns][i % columns] = matrix[j / columns][j % columns];
matrix[j / columns][j % columns] = tmp;
}
/** Just some test code. */
public static void main(String[] args) throws Exception {
double[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
shuffle(matrix, 3, new Random());
for (int r = 0; r < matrix.length; r++) {
for (int c = 0; c < matrix[r].length; c++) {
System.out.print(matrix[r][c] + "\t");
}
System.out.println();
}
}
/**对每行具有相同列数的二维数组进行无序排列*/
公共静态void shuffle(双[][]矩阵,int列,随机rnd){
int size=matrix.length*列;
对于(int i=size;i>1;i--)
交换(矩阵、列、i-1、rnd.nextInt(i));
}
/**
*交换二维数组中的两个条目,其中i和j是一维索引,查看
*从左到右、从上到下排列。
*/
公共静态无效交换(双[][]矩阵,int列,int i,int j){
双tmp=矩阵[i/列][i%列];
矩阵[i/列][i%列]=矩阵[j/列][j%列];
矩阵[j/列][j%列]=tmp;
}
/**只是一些测试代码*/
公共静态void main(字符串[]args)引发异常{
双[]矩阵={{1,2,3},{4,5,6},{7,8,9},{10,11,12};
洗牌(矩阵,3,新随机();
对于(int r=0;r
要将n个元素(索引0..n-1)的m个数组中的一个二维数组洗牌,请执行以下操作:
灵感来自-感谢奥比切雷的评论我只需要做一个带有解码功能的Fisher-Yates混洗,从1D索引到2D索引。也许是更好的办法。
/** Shuffles a 2D array with the same number of columns for each row. */
public static void shuffle(double[][] matrix, int columns, Random rnd) {
int size = matrix.length * columns;
for (int i = size; i > 1; i--)
swap(matrix, columns, i - 1, rnd.nextInt(i));
}
/**
* Swaps two entries in a 2D array, where i and j are 1-dimensional indexes, looking at the
* array from left to right and top to bottom.
*/
public static void swap(double[][] matrix, int columns, int i, int j) {
double tmp = matrix[i / columns][i % columns];
matrix[i / columns][i % columns] = matrix[j / columns][j % columns];
matrix[j / columns][j % columns] = tmp;
}
/** Just some test code. */
public static void main(String[] args) throws Exception {
double[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
shuffle(matrix, 3, new Random());
for (int r = 0; r < matrix.length; r++) {
for (int c = 0; c < matrix[r].length; c++) {
System.out.print(matrix[r][c] + "\t");
}
System.out.println();
}
}
for h from m - 1 downto 0 do
for i from n − 1 downto 1 do
j ← random integer with 0 ≤ j ≤ i
k ← random integer with 0 ≤ k ≤ h
exchange a[k[j]] and a[h[i]]