Janilabo

Janilabo | RandomRangeEx() Testing

Sep 6th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.23 KB | None | 0 0
  1. function RandomRangeEx2(aFrom, aTo, amount: Integer; duplicates: Boolean): TIntArray;
  2. var
  3.   a, r, i, n: Integer;
  4. begin
  5.   if (amount > 0) then
  6.   case duplicates of
  7.     True:  
  8.     begin
  9.       SetLength(Result, amount);
  10.       for i := 0 to (amount - 1) do
  11.         Result[i] := RandomRange(aFrom, aTo);
  12.     end;
  13.     False:
  14.     begin
  15.       if (aFrom > aTo) then
  16.         Swap(aFrom, aTo);    
  17.       a := IAbs(aTo - aFrom);
  18.       if (a < amount) then
  19.         amount := a;
  20.       if (amount < 1) then
  21.         Result := [0]
  22.       else
  23.         for r := 0 to (amount - 1) do
  24.           repeat  
  25.             n := RandomRange(aFrom, aTo);
  26.             if not TIAContains(Result, n) then
  27.               i := TIAAppend(Result, n);
  28.           until (i >= r);    
  29.     end;
  30.   end;
  31. end;
  32.  
  33. function RandomRangeEx4(aFrom, aTo, amount: Integer; duplicates: Boolean): TIntArray;
  34. var
  35.   a, r, i, n: Integer;
  36.   tmp: TIntArray;
  37. begin
  38.   if (amount > 0) then
  39.   case duplicates of
  40.     True:  
  41.     begin
  42.       SetLength(Result, amount);
  43.       for i := 0 to (amount - 1) do
  44.         Result[i] := RandomRange(aFrom, aTo);
  45.     end;
  46.     False:
  47.     begin
  48.       if (aFrom > aTo) then
  49.         Swap(aFrom, aTo);    
  50.       a := IAbs(aTo - aFrom);
  51.       if (a < amount) then
  52.         amount := a;
  53.       if (amount < 1) then
  54.       begin
  55.         Result := [aFrom];
  56.         Exit;
  57.       end;                          
  58.       SetLength(tmp, (aTo - aFrom));
  59.       for i := aFrom to (aTo - 1) do
  60.         tmp[(i - aFrom)] := i;      
  61.       SetLength(Result, amount);
  62.       for i := 0 to (amount - 1) do
  63.       begin  
  64.         r := Random((amount - 1) - i);
  65.         Result[i] := tmp[r];  
  66.         Delete(tmp, r, 1);
  67.       end;    
  68.     end;
  69.   end;
  70. end;
  71.  
  72. var
  73.   tm: Integer;
  74.   TIA: TIntArray;
  75.  
  76. begin
  77.   ClearDebug;  
  78.   tm := GetSystemTime;
  79.   TIA := RandomRangeEx2(-99999, 99999, 200000, False);
  80.   WriteLn('RandomRangeEx2: ' + IntToStr(Length(TIA)) + ' items!' + ' [' + IntToStr(GetSystemTime - tm) + ' ms.]');
  81.   SetLength(TIA, 0);  
  82.   tm := GetSystemTime;
  83.   TIA := RandomRangeEx4(-99999, 99999, 200000, False);
  84.   WriteLn('RandomRangeEx4: ' + IntToStr(Length(TIA)) + ' items!' + ' [' + IntToStr(GetSystemTime - tm) + ' ms.]');
  85.   SetLength(TIA, 0);  
  86. end.
Advertisement
Add Comment
Please, Sign In to add comment