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 an M x N matrix with random numbers in Pascal

1 Answer

0 votes
program RandomMatrix;

const
  ROWS = 4;
  COLS = 5;

type
  TMatrix = array[1..ROWS, 1..COLS] of Integer;

procedure PrintMatrix(matrix: TMatrix; rows, cols: Integer);
var
  i, j: Integer;
begin
  for i := 1 to rows do
  begin
    for j := 1 to cols do
      write(matrix[i, j]:4);
    writeln;
  end;
end;

{ Generate a random integer between min and max inclusive }
function GenerateRandomInteger(min, max: Integer): Integer;
begin
  GenerateRandomInteger := min + Random(max - min + 1);
end;

{ Fill matrix with random integers }
procedure GenerateRandomMatrix(var matrix: TMatrix; rows, cols: Integer);
var
  i, j: Integer;
begin
  Randomize; { seed random generator }
  for i := 1 to rows do
    for j := 1 to cols do
      matrix[i, j] := GenerateRandomInteger(1, 100);
end;

var
  matrix: TMatrix;

begin
  GenerateRandomMatrix(matrix, ROWS, COLS);

  PrintMatrix(matrix, ROWS, COLS);
end.



(*
run:

  71  16  68  26  39
  59  85  74  24  32
  69  96   8  53   1
  14  90  36  38  61

*)

 



answered Nov 22, 2025 by avibootz
...