Task: I need an algorithm that would make a list of numbers that are unique, yet fall within a certain range of numbers. No number can be duplicated in this list.

Example: Given a range of numbers from 1 to 100, pick 20 numbers in this range at random, and store them in a list or array. None of the 20 numbers selected can occur more than once in this list/array.

Variables will be:

1) The quantity of unique numbers needed (e.g. 20 in the above example)

2) The range of numbers (e.g, 1-100)

If the algorithm is run twice, it should not produce the same results.

Can anyone provide such an algorithm?

procedure CompRandArray(var ArrayToFill: Array of Integer; LowRange, HighRange: Integer);

var

I, J: integer;

HaveDup: boolean;

begin

Randomize;

for I := Low(ArrayToFill) to High(ArrayToFill) do

begin

Repeat

ArrayToFill[I] := Random(HighRange);

Until ArrayToFill[I] > LowRange;

Repeat

HaveDup := False;

for J := Low(ArrayToFill) to High(ArrayToFill) do

begin

if (ArrayToFill[J] = ArrayToFill[I]) and (I <> J) then

begin

ArrayToFill[I] := Random(HighRange);

HaveDup := True;

break;

end;

end;

Until HaveDup = False;

end;

end;