var
Form1: TForm1;
num:array[0..5] of integer;
implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
j:integer;
begin
for i := 0 to 4 do
begin
repeat
for j := 0 to i do
if num[j]=num[i] then
num[i]:=random(23);
until(num[j]<>num[i]);
end; edit1.Text:=inttostr(num[0]);
edit2.Text:=inttostr(num[1]);
edit3.Text:=inttostr(num[2]);
edit4.Text:=inttostr(num[3]);
edit5.Text:=inttostr(num[4]);
end;
这是我的代码,但总有重复的数字出现
Form1: TForm1;
num:array[0..5] of integer;
implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
j:integer;
begin
for i := 0 to 4 do
begin
repeat
for j := 0 to i do
if num[j]=num[i] then
num[i]:=random(23);
until(num[j]<>num[i]);
end; edit1.Text:=inttostr(num[0]);
edit2.Text:=inttostr(num[1]);
edit3.Text:=inttostr(num[2]);
edit4.Text:=inttostr(num[3]);
edit5.Text:=inttostr(num[4]);
end;
这是我的代码,但总有重复的数字出现
NumList: TList;
I,iIndex: Integer;begin
NumList := TList.Create;
try
for I := 0 to 23 do
NumList.Add(Pointer(i));
for I := 0 to 5 - 1 do begin
Randomize;
iIndex := Random(NumList.Count);
ShowMessage(IntToStr(Integer(NumList.Items[iIndex])));
NumList.Delete(iIndex);;
end;
finally
NumList.Free;
end;
end;
NumList: TList;
I,iIndex: Integer;
strNum: AnsiString;
begin
NumList := TList.Create;
try
for I := 0 to 23 do
NumList.Add(Pointer(i));
for I := 0 to 5 - 1 do begin
Randomize;
iIndex := Random(NumList.Count);
strNum :=IntToStr(Integer(NumList.Items[iIndex]));
case I of
0: Edit1.Text := strNum;
1: Edit2.Text := strNum;
2: Edit3.Text := strNum;
3: Edit4.Text := strNum;
4: Edit5.Text := strNum;
end;
NumList.Delete(iIndex);;
end;
finally
NumList.Free;
end;
end;
NumList.Add(Pointer(i));
list可以是字符、数字或者其他的列表类,直接指向指针
范围在0至RAND_MAX 间