Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function RandomRangeEx2(aFrom, aTo, amount: Integer; duplicates: Boolean): TIntArray;
- var
- a, r, i, n: Integer;
- 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
- Result := [0]
- else
- for r := 0 to (amount - 1) do
- repeat
- n := RandomRange(aFrom, aTo);
- if not TIAContains(Result, n) then
- i := TIAAppend(Result, n);
- until (i >= r);
- end;
- end;
- end;
- function RandomRangeEx4(aFrom, aTo, amount: Integer; duplicates: Boolean): TIntArray;
- var
- a, r, i, n: Integer;
- tmp: TIntArray;
- 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(tmp, (aTo - aFrom));
- for i := aFrom to (aTo - 1) do
- tmp[(i - aFrom)] := i;
- SetLength(Result, amount);
- for i := 0 to (amount - 1) do
- begin
- r := Random((amount - 1) - i);
- Result[i] := tmp[r];
- Delete(tmp, r, 1);
- end;
- end;
- end;
- end;
- var
- tm: Integer;
- TIA: TIntArray;
- begin
- ClearDebug;
- tm := GetSystemTime;
- TIA := RandomRangeEx2(-99999, 99999, 200000, False);
- WriteLn('RandomRangeEx2: ' + IntToStr(Length(TIA)) + ' items!' + ' [' + IntToStr(GetSystemTime - tm) + ' ms.]');
- SetLength(TIA, 0);
- tm := GetSystemTime;
- TIA := RandomRangeEx4(-99999, 99999, 200000, False);
- WriteLn('RandomRangeEx4: ' + IntToStr(Length(TIA)) + ' items!' + ' [' + IntToStr(GetSystemTime - tm) + ' ms.]');
- SetLength(TIA, 0);
- end.
Advertisement
Add Comment
Please, Sign In to add comment