For example, I have 2d array:
int[,] array = new int[3,3] {{1,2,3}, {4,5,6} {7,8,9}}
1 2 3
4 5 6
7 8 9
I want to shuffle the order of the row like this
4 5 6
7 8 9
1 2 3
>Solution :
You can use the Fisher-Yates shuffle to swap the "rows" of the array. I used this answer for a 1d array and converted it to work with a 2d array:
public static void Shuffle(Random random, int[,] arr)
{
int height = arr.GetUpperBound(0) + 1;
int width = arr.GetUpperBound(1) + 1;
for (int i = 0; i < height; ++i)
{
int randomRow = random.Next(i, height);
for (int j = 0; j < width; ++j)
{
int tmp = arr[i, j];
arr[i, j] = arr[randomRow, j];
arr[randomRow, j] = tmp;
}
}
}