Janilabo

Janilabo | RandomRangeEx5()

Sep 7th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.79 KB | None | 0 0
  1. function RandomRangeEx5(aFrom, aTo, amount: Integer; duplicates: Boolean): TIntArray;
  2. var
  3.   a, d, e, m, r, i, t: Integer;
  4.   tmp: TIntArray;
  5.   b: Boolean;
  6. begin
  7.   if (amount > 0) then
  8.   case duplicates of
  9.     True:  
  10.     begin
  11.       SetLength(Result, amount);
  12.       for i := 0 to (amount - 1) do
  13.         Result[i] := RandomRange(aFrom, aTo);
  14.     end;
  15.     False:
  16.     begin
  17.       if (aFrom > aTo) then
  18.         Swap(aFrom, aTo);    
  19.       a := IAbs(aTo - aFrom);
  20.       if (a < amount) then
  21.         amount := a;
  22.       if (amount < 1) then
  23.       begin
  24.         Result := [aFrom];
  25.         Exit;
  26.       end;        
  27.       SetLength(Result, amount);
  28.       m := IAbs(aFrom - aTo);
  29.       if (m > 10) then
  30.         b := (m > Trunc(amount * 1.1));
  31.       case b of
  32.         True:
  33.         begin          
  34.           d := (Trunc(m / amount) + 3);
  35.           e := aFrom;
  36.           for i := 0 to (amount - 1) do
  37.           begin                            
  38.             r := Random(d);        
  39.             IncEx(e, r);      
  40.             IncEx(t, (r + 1));
  41.             Result[i] := (e + i);
  42.             Swap(Result[Random(i)], Result[Random(i)]);        
  43.             d := (Trunc(((m - t) / (amount - i))) * 2);
  44.           end;
  45.           for i := 0 to (amount div 2) do
  46.             Swap(Result[Random(amount)], Result[Random(amount)]);
  47.         end;
  48.         False:
  49.         begin    
  50.           SetLength(tmp, (aTo - aFrom));
  51.           for i := aFrom to (aTo - 1) do
  52.             tmp[(i - aFrom)] := i;      
  53.           for i := 0 to (amount - 1) do
  54.           begin  
  55.             r := Random((aTo - aFrom) - i);  
  56.             Result[i] := tmp[r];  
  57.             Delete(tmp, r, 1);
  58.           end;
  59.           SetLength(tmp, 0);
  60.         end;
  61.       end;                        
  62.     end;
  63.   end;
  64. end;
Advertisement
Add Comment
Please, Sign In to add comment