SRL Archive - SRL Archives

ShuffleStr(var Arr: TStringArray), ShuffleInt(var Arr: TIntegerArray)


marpis

2009-08-23 19:16:30

['a', 'b', 'c', 'd'] -> ['b', 'c', 'a', 'd']
[1, 2, 3, 4] -> [4, 1, 3 , 2]


procedure ShuffleStr(var Arr: TStringArray);
var
tmp: TStringArray;
r: integer;
begin
while Length(tmp) < Length(arr) do
begin
r := random(Length(Arr));
if not InStrArr(Arr[r], tmp, true) then
begin
SetLength(tmp, Length(tmp)+1);
tmp[High(tmp)] := Arr[r];
end;
end;
for r := 0 to High(Arr) do
Arr[r] := tmp[r];
end;

procedure ShuffleInt(var Arr: TIntegerArray);
var
tmp: TIntegerArray;
r: integer;
begin
while Length(tmp) < Length(arr) do
begin
r := random(Length(Arr));
if not InIntArray(tmp, Arr[r]) then
begin
SetLength(tmp, Length(tmp)+1);
tmp[High(tmp)] := Arr[r];
end;
end;
for r := 0 to High(Arr) do
Arr[r] := tmp[r];
end;

ShowerThoughts

2009-08-23 19:49:51

I am pretty sure this already exists for integers, can't recall the Name too. :(