Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to create a 20x20 matrix with random numbers between 1 and 100 in C#

1 Answer

0 votes
using System;

public class Program
{
    static int rows = 20;
    static int columns = 20;
        
    public static void PrintMatrix(int[,] matrix) {
        for (int i = 0; i < matrix.GetLength(0); i++) {
            for (int j = 0; j < matrix.GetLength(1); j++) {
                Console.Write(matrix[i, j].ToString().PadLeft(4) + " ");
            }
            Console.WriteLine();
        }
    }
    
	private static int[,] generateRandomInteger(int size) {
	    Random random = new Random();

        int[,] matrix = new int[rows, columns];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                matrix[i, j] = random.Next(1, 101); 
            }
        }

		return matrix;
	}

    static void Main(string[] args)
    {
        int[,] matrix = generateRandomInteger(20);


        PrintMatrix(matrix);
    }
}




/*
run:

  93   27   65   10   44   57   62   91   88    8   15   58   68   95   19   58   82   67   11   69 
  71   14   72    3   71   68   13   64   67   41   99   40   84   12   37   40   37   13   37   60 
  46   73   44   61   61   53   53   88   11   91    3   79   87   25   67   80   55   62   40   76 
  44   99   25   47   10   75   74   56   59   80   22   69   30   51   23   98   70   11   43   18 
  16   26   54   76   39   20   54   59   45   57   85   76   74   62   17   48   49   98   52   86 
  79   97   30   31   70   34   50   36    3   69   10   44   19   23   60   18   45   49    8   90 
  22   16   12    2   96   47   96   68   34   75   50   73   60   58   39   20   97   23    7    5 
  71   18   57   76   48   41   57   51    2   99    4  100   90   63   64   64   85   29   36   24 
  38   82    2   28   20   37   85   61   84   41   22   22   42    4   19    5   60   36   54   55 
  90   45   67   35   72   50   84   98   95   76   35   68   87   84   67   90   16   29   57   11 
  57   96   67   62   78   82   59   87   44   59    4   50   75   82   34   93   16   68   57   70 
  54   88   67    9    6   54   35   59   37   30   90   32   79   43   99   95   79    5   58   90 
  92   98   54   36   72   86   94    6   51   74   74   49   72   87   58   70   30   58   57   24 
  47  100   50   15   70   73   71   32   83   40   15   12   10   68   79   57   34   31   38   20 
  61   30    9   63   56   42   60   93   86   30   65   21   48   34   44   92   48   40   67   99 
  15   62   23   11   60   63   39    4    9    1   36   99   21   37   64   18   92   88   60   29 
  13   79   47   54   75   95   64   76   25   87   10   95   65   40    5   99    7   98    4   93 
   3   57   85   85   94   67    1   12   71   26  100   61   81   38   86   37   16   70   37   65 
 100   64   80   22   92   42   35   81   32   65   12   95   84   67   27   57   94   62   61    8 
  95   52    5   99   88   50   15   27   54   69   84   38   61   40   93   39   77   64   93   52 
  
*/

 



answered Nov 2, 2023 by avibootz
edited Nov 2, 2023 by avibootz
...