Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function RandomRangeEx5(aFrom, aTo, amount: Integer; duplicates: Boolean): TIntArray;
- var
- a, d, e, m, r, i, t: Integer;
- tmp: TIntArray;
- b: Boolean;
- begin
- if (amount > 0) then
- case duplicates of
- True:
- begin
- SetLength(Result, amount);
- for i := 0 to (amount - 1) do
- Result[i] := RandomRange(aFrom, aTo);
- end;
- False:
- begin
- if (aFrom > aTo) then
- Swap(aFrom, aTo);
- a := IAbs(aTo - aFrom);
- if (a < amount) then
- amount := a;
- if (amount < 1) then
- begin
- Result := [aFrom];
- Exit;
- end;
- SetLength(Result, amount);
- m := IAbs(aFrom - aTo);
- if (m > 10) then
- b := (m > Trunc(amount * 1.1));
- case b of
- True:
- begin
- d := (Trunc(m / amount) + 3);
- e := aFrom;
- for i := 0 to (amount - 1) do
- begin
- r := Random(d);
- IncEx(e, r);
- IncEx(t, (r + 1));
- Result[i] := (e + i);
- Swap(Result[Random(i)], Result[Random(i)]);
- d := (Trunc(((m - t) / (amount - i))) * 2);
- end;
- for i := 0 to (amount div 2) do
- Swap(Result[Random(amount)], Result[Random(amount)]);
- end;
- False:
- begin
- SetLength(tmp, (aTo - aFrom));
- for i := aFrom to (aTo - 1) do
- tmp[(i - aFrom)] := i;
- for i := 0 to (amount - 1) do
- begin
- r := Random((aTo - aFrom) - i);
- Result[i] := tmp[r];
- Delete(tmp, r, 1);
- end;
- SetLength(tmp, 0);
- end;
- end;
- end;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment