# Unique Random Numbers

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.

[color=Red][b]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.[/b][/color]

[code]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]);

End.

• There may be 2 things that I can think of that might help. I'm not sure what a 216 error is and maybe you should tell what it is.

[code] [color=Red]If j=arraymax[/color][/code]
[code] begin[color=Green]
randomize;[/color] {this sets up the random number generator}
.
.
.[color=Green]
if j >= arraymax [/color][/code]
It could be that one of your variables is going beyond the range of the array index.
• [code][color=Blue]const max_no=20;
max_value=100;

var a:array[1..max_no] of byte;
cl:byte;

function check(b:byte):boolean;
var trig:boolean;
i:byte;
begin
trig:=false;
for i:=1 to cl do if b=a[i] then begin trig:=true;break;end;
check:=trig;
end;

var i,j:byte;

begin
randomize;
fillchar(a,sizeof(a),0);
cl:=0;
for i:=1 to max_no do begin
repeat j:=succ(random(max_value));until not(check(j));
inc(cl);
a[cl]:=j;
writeln(j);
end;
end.
[/color][/code]

I had somehow fogot to randomize at the beginning.

[color=Black]That was surely wrong and your suggestion j>=arraymax makes much more sense so thanks again.[/color]

However, the error is still being given.
I googled run time error codes and error 216 is this:

[color=Orange]216 General protection fault [/color]

I had somehow fogot to randomize at the beginning.

[color=Black]That was surely wrong and your suggestion j>=arraymax makes much more sense so thanks again.[/color]

However, the error is still being given.
I googled run time error codes and error 216 is this:

[color=Orange]216 General protection fault [/color]

I had somehow fogot to randomize at the beginning.

[color=Black]That was surely wrong and your suggestion j>=arraymax makes much more sense so thanks again.[/color]

However, the error is still being given.
I googled run time error codes and error 216 is this:

[color=Orange]216 General protection fault [/color]
• Sry I posted the same thing more than once but the website jammed for some time
• Thank you, I copied your code and it worked.

However, could you please make some comments near important parts of the code cause I don't understand everything.

Firstly, I haven't ever used byte as a variable type and google doesn't explain much so if u can pls explain what's its use it would really help.

Secondly, I can't really manage to understand what the 2 global variables actually do, especially "cl".

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!