I made a two dimensional array in c # with random numbers but I want the numbers are not repeated For example, a successful output giving the input 4 (x), 4 (y), 15 (maxElem), would be:
14|8|1|7
3|13|2|4
2|6|12|8
10|9|4|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TwoDimensionArray
{
class Program
{
static void Main(string[] args)
{
int x = 4;
int y = 4;
int[,] z = new int[x, y];
Random r = new Random();
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
z[i, j] = r.Next(1, 15);
}
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (i == 3 && j == 3)
{
Console.Write(" ");
break;
}
else
{
if (z[i, j] > 9)
Console.Write(z[i, j] + " ");
else
Console.Write(z[i, j] + " ");
}
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}