Unique random numbers in range problem:

Unique random numbers in range problem:

I have:
Var
i: Integer;
RandomNumbers : array of integer;

Begin
SetLength(RandomNumbers, 5);
Randomize;
for i := 0 to 4 do
begin
RandomNumbers[i] := 1 +RandomNumbers(1,200);
ShowMessage('Random Numbers are = '+IntToStr(RandomNumbers[i]));
end;

Problem is that sometimes I get repeat values. I need unique values.
How to achieve this?

Thx.
Tomislav

Comments

  1. It seams that I get an answer... BIG THX to ALL!!

    ReplyDelete
  2. Maybe what you want is sampling a uniform distribution of integers without replacement. Easy to implement. statisticshowto.com - Sampling With Replacement / Sampling Without Replacement

    ReplyDelete
  3. Take an array of values from 1 to 200, shuffle it, read the first N values of the shuffled array. In fact, you can abort the shuffle after N steps.

    ReplyDelete

Post a Comment