Hi,
I am writing a program which involves generating 20 random numbers in the range of 1..100 and display them on the screen. However, each number must be unique so there should be no duplicates. So if a number has been genertaed, it cannot be generated again.
Below is my source code which I managed to write. However I have 2 problems with it. When i set the randmax to 100, a 216 run time error is given. When i set it to a low number such as 10, the program works but nothing is displayed.
Could anyone help please?
Edit: I finally managed to solve the problem; I had to do it in a different way but it worked. Thanks for your help it helped me a lot.
Program Rand_Unique;
Uses Crt;
Const
arraymax=20;
randmax=100;
Var
i, j, rand : Integer;
nums : Array[1..arraymax] of Integer;
check : Boolean;
Begin
TextBackground(White);
TextColor(Black);
Clrscr;
j:=1;
i:=1;
nums[1]:=0;
check:=False;
Repeat
rand:=random(randmax)+1;
While (j<arraymax) or (check=False) do
Begin
If nums[j]=rand
then check:=True
else j:=j+1;
End;
If j=arraymax
then Begin
nums[i]:=rand;
i:=i+1;
End;
Until(i=arraymax);
For i:=1 to arraymax do
Writeln(nums[i]);
Readkey;
End.