'How the choose an 2d array randomly from a 3d array? [duplicate]

As you can see there are 7 'figures' in my 3d array. How can I randomly choose an 2d array from the 3d array? If I execute my code now, it will take a random number inside the 2d array but it must give the whole array as the output.

// Online C# Editor for free
// Write, Edit and Run your C# code using C# Online Compiler
    
using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        Random rnd = new Random();
         int[,,] pieces ={   {   {0, 1, 0, 0},
                                {0, 1, 0, 0},
                                {0, 1, 0, 0},
                                {0, 1, 0, 0}   },

                            {   {0, 1, 0, 0},
                                {0, 1, 0, 0},
                                {0, 1, 1, 0},
                                {0, 0, 0, 0}   },

                            {   {0, 0, 1, 0},
                                {0, 0, 1, 0},
                                {0, 1, 1, 0},
                                {0, 0, 0, 0}   },

                            {   {0, 0, 0, 0},
                                {1, 1, 1, 0},
                                {0, 1, 0, 0},
                                {0, 0, 0, 0}   },

                            {   {0, 0, 0, 0},
                                {0, 1, 1, 0},
                                {0, 1, 1, 0},
                                {0, 0, 0, 0}   },

                            {   {0, 0, 0, 0},
                                {0, 0, 1, 1},
                                {0, 1, 1, 0},
                                {0, 0, 0, 0}  },

                            {   {0, 0, 0, 0},
                                {1, 1, 0, 0},
                                {0, 1, 1, 0},
                                {0, 0, 0, 0}    }   };
            //int[,] returnPiece = pieces[rnd.Next(1, 8)];
            int a = pieces[rnd.Next(pieces.GetLength(0)), rnd.Next(pieces.GetLength(1)), rnd.Next(pieces.GetLength(2))];
            Console.WriteLine(a);
    }
}


Solution 1:[1]

a) If you want to use [,,], then you have to project each element into needed massive. Just like you're doing it with [,], to get a row you have to write something like this:

var pieces = new[,] { {0, 0, 0, 0},
                      {1, 1, 0, 0},
                      {0, 1, 1, 1},
                      {0, 0, 0, 0} };

var colLength = pieces.GetLength(0);
var rowLength = pieces.GetLength(1);
var row = new int[rowLength];
var rand = new Random().Next(colLength);

for (int i = 0; i < rowLength; i++)
    row[i] = pieces[rand, i];

foreach (var i in row)
    Console.Write($"{i} ");

Then scale it on 3d array:

var pieces = new[,,] { { {0, 1, 0, 0},
                         {0, 1, 0, 0},
                         {0, 1, 0, 0},
                         {0, 1, 0, 0} },
                       { {0, 1, 0, 0},
                         {0, 1, 0, 0},
                         {0, 1, 1, 0},
                         {0, 0, 0, 0} },
                       { {0, 0, 1, 0},
                         {0, 0, 1, 0},
                         {0, 1, 1, 0},
                         {0, 0, 0, 0} },
                       { {0, 0, 0, 0},
                         {1, 1, 1, 0},
                         {0, 1, 0, 0},
                         {0, 0, 0, 0} },
                       { {0, 0, 0, 0},
                         {0, 1, 1, 0},
                         {0, 1, 1, 0},
                         {0, 0, 0, 0} },
                       { {0, 0, 0, 0},
                         {0, 0, 1, 1},
                         {0, 1, 1, 0},
                         {0, 0, 0, 0} },
                       { {0, 0, 0, 0},
                         {1, 1, 0, 0},
                         {0, 1, 1, 0},
                         {0, 0, 0, 0} } };

var xLength = pieces.GetLength(0);
var yLength = pieces.GetLength(1);
var zLength = pieces.GetLength(2);
var mass = new int[yLength, zLength];
var rand = new Random().Next(xLength);

for (int i = 0; i < yLength; i++)
{
    for (int y = 0; y < zLength; y++)
    {
        mass[i, y] = pieces[rand, i, y];
        Console.Write($"{mass[i, y]} ");
    }

    Console.WriteLine();
}

b) You can declare your massive as int[][][] and just take pieces[new Random().Next(pieces.GetLength(0))] for an example. See this answer to get more information about this method.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RelativeLayouter