lundi 2 mars 2020

Using Random class to randomize 2d array

I am trying to randomize a sett of predetermine elements in a 2d array.

using System;

namespace array
{
    public class tiles
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            int[,] tilearr = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } };

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write(tilearr[i, j] + " ");
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
    }
}

My problem is if I do something like tilearr[i, j] = random.Next(0, 8); it randomizes the array but doesn't care if there are any duplicates of the same element.

2 6 7
1 1 3
2 7 0

what I am after is more like this:

2 4 8  1 3 0
0 3 1  5 6 8 
6 7 5, 2 4 7



Aucun commentaire:

Enregistrer un commentaire