Limiting the amount of times numbers occur from a random number generator.

Hey!

For a project I need to populate a 6×6 array with 18 numbers, but each repeated just twice to fill out the 36 slot array.

I have the code to produce 18 random numbers, but as it is random, it often produces the same number more than twice.

Is there a way to generate random numbers but limit the repetition? Or is use a random number generator the wrong approach?

Here is the code I have, it also displays it for me to see it's output.


public class Program

{ public void Main()

{ Random rnd = new Random(); int[,] Grid = new int[6, 6]; for (int row = 0; row < 6; row++) { for (int col = 0; col < 6; col++) { Grid[row, col] = rnd.Next(1, 18); Console.WriteLine("[{0}, {1}] = {2}", row, col, Grid[row, col]); } } } 

}


Any help is appreciated 🙂

by Modulite via /r/csharp

Leave a Reply