How to fill an array with different random numbers from 1000 to 9999?

Viewed 441

I'm trying to fill an array with different numbers from 1000 to 9999.

My problem is that my code is filling the array with the same number. Here is my approach:

repeat
  Write('Enter an Integer from 5 to 20: '); Readln(n)
until (n in [5..20]);

for i := 1 to n do 
begin
  T[i] := Random(9000)+1000; // 1000 to 9999
  Randomize
end;

Writeln('___________________________________');
for i := 1 to n do 
  Write(T[i], ' | '  );
Writeln;    
Writeln('___________________________________');
1 Answers

You should call Randomize() before the actual loop:

Randomize();
for i := 1 to n do
begin
  T[i] := Random(9000) + 1000;
end;

Randomize initializes the random number generator

Related