简体   繁体   English

改组二维数组

[英]Shuffling 2D Array

I have a 2D string array.我有一个二维字符串数组。

I just want to randomize the rows order and shuffle the items within each row.我只想随机化行顺序并随机排列每行中的项目。

So for example:例如:

[1,2
 3,4]

I want it to be as below, row pairs stay same here (order change is valid):我希望它如下所示,行对在这里保持不变(订单更改有效):

[4,3
 1,2] or
[3,4
 2,1] or
[4,3
 2,1] or
[1,2
 3,4] or
[2,1
 3,4] or
[2,1
 4,3] or
[3,4
 1,2] 

and i want to avoid below shuffling because i'll read my array row by row, i want to keep my rows to contain same elements.我想避免以下洗牌,因为我将逐行读取我的数组,我想让我的行包含相同的元素。 I want to keep my row pairs.我想保留我的行对。 Below, my [1,2] and [3,4] rows does not exist anymore :下面,我的 [1,2] 和 [3,4] 行不再存在:

[1,3
 2,4] or 
[3,1
 4,2] or 
[3,1
 2,4] or
[1,4
 2,3] ....

So here is my array:所以这是我的数组:

array_to_shuffle = new string[len_2d,2];
Shuffle(array_to_shuffle);

and the function i need help :以及我需要帮助的功能:

    public void Shuffle(Random rand, string[,] array)
    {
        rand = new Random();
        int[] randomised_array = new int[len_2d];

        for (int i = 0; i < len_2d; i++)
        {
            randomised_array[i] = i;
        }
        int[] MyRandomArray = randomised_array.OrderBy(x => rand.Next()).ToArray();
        string tmp1 = String.Empty;
        string tmp2 = String.Empty;

        array[MyRandomArray[0], 0] = tmp1;
        array[MyRandomArray[0], 1] = tmp2;

        array[MyRandomArray[0], 0] = array[MyRandomArray[1],0];
        array[MyRandomArray[0], 1] = array[MyRandomArray[1],1];
        array[MyRandomArray[1], 0] = tmp2;
        array[MyRandomArray[1], 1] = tmp1;


    }

Thank you everyone...谢谢大家...

Try this code (necessary comments are in code):试试这个代码(必要的注释在代码中):

static void Main(string[] args)
{
    int[,] matrix = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 9, 10 }, { 11, 12 } };
    matrix = Shuffle(matrix);
}

static T[,] Shuffle<T>(T[,] matrix)
{
    int howManyRows = matrix.GetLength(0);
    int howManyColumns = matrix.GetLength(1);
    T[,] randomizedMatrix = new T[howManyRows, howManyColumns];
    //we will use those arrays to randomize indexes
    int[] shuffledRowIndexes = Enumerable.Range(0, howManyRows).ToArray();
    int[] shuffledColumnIndexes = Enumerable.Range(0, howManyColumns).ToArray();
    Random rnd = new Random();
    shuffledRowIndexes = shuffledRowIndexes.OrderBy(x => rnd.Next()).ToArray();

    for (int i = 0; i < howManyRows; i++)
    {
        // at every loop we get new randomized column idexes, so every row will be shuffled independently
        shuffledColumnIndexes = shuffledColumnIndexes.OrderBy(x => rnd.Next()).ToArray();
        for (int j = 0; j < howManyColumns; j++)
            randomizedMatrix[i, j] = matrix[shuffledRowIndexes.ElementAt(i), shuffledColumnIndexes.ElementAt(j)];
    }

    return randomizedMatrix;
}

Helpful articles:有用的文章:

Best way to randomize an array with .NET 使用 .NET 随机化数组的最佳方法

What are the differences between a multidimensional array and an array of arrays in C#? C#中的多维数组和数组数组有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM