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,959 questions

51,901 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;

{ Print matrix to console }
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 }
function GenerateRandomInteger(min, max: Integer): Integer;
begin
  { Turbo Pascal random returns [0..max-1], so scale accordingly }
  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
  { Generate random matrix }
  GenerateRandomMatrix(matrix, ROWS, COLS);

  { Print matrix }
  PrintMatrix(matrix, ROWS, COLS);
end.



(*
run:

  91  30  40  93  75
  30  63  56  80  73
   6  35  86  36  31
  96  49  99  37   4

*)

 



answered Nov 22, 2025 by avibootz
...