假设数组cards[1..54]存放的是牌的的花色,TCard是花色的类型
那么可以这样
procedure get4card(var c1,c2,c3,c4:TCard);
var
card:TCard;
I,J:Integer;
begin
for J:=0 to 3 do
begin
I:=Radom(53-J)+1;
card:=Cards[I];
Cards[I]:=Cards[54-J]
Cards[54-J]:=card;
end;
c1:=Cards[54];
c2:=Cards[53];
c3:=Cards[52];
c4:=Cards[51];
end;
可以给分了吧!
procedure TForm1.FormCreate(Sender: TObject);
var
I: Integer;
begin
Randomize;
Memo1.Clear;
for I := 0 to 53 do Memo1.Lines.Add(IntToStr(I));
end;
function Sortition1(mSource: TStrings; mDest: TStrings;
mCount: Integer): Boolean;
var
I, J: Integer;
begin
Result := True;
try
mDest.Clear;
for I := 1 to mCount do begin
J := Random(mSource.Count);
mDest.Add(mSource[J]);
mSource.Move(J, mSource.Count - I);
end;
except
Result := False;
end;
end; { Sortition1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
Sortition1(Memo1.Lines, Memo2.Lines, 4);
end;
function Sortition2(mSource: TStrings; mDest: TStrings;
mCount: Integer): Boolean;
var
I, J, K, L: Integer;
vCnt: Integer;
begin
Result := True;
try
mDest.Clear;
vCnt := 0;
for I := 1 to mCount do begin
J := Random(mSource.Count - vCnt);
L := 0;
for K := 0 to mSource.Count - 1 do
if mDest.IndexOf(mSource[K]) < 0 then begin
if L = J then begin
mDest.Add(mSource[J]);
Inc(vCnt);
Break;
end;
Inc(L);
end;
end;
except
Result := False;
end;
end; { Sortition2 }
var No: Integer;
NoSet: Set of Integer;
begin
Randomize;
No := Random(ADOTable.RecordCount);
if No in NoSet then
begin
ADOTable.Locate('No',[No],[]);
Include(NoSet,No);
end else
...
end
...
end;