procedure TForm1.Timer1Timer(Sender: TObject); var RedArr: array[1..6] of Byte; i,j: Integer; value: Byte; b: Boolean; function MakeRandom(Rang: Integer): Byte; //生成<>0的随机数 begin repeat result := Random(Rang); until result<>0; end; procedure compositor(var L: array of byte); //排序 var i,j:byte; v:byte; begin for i:=low(L) to high(L) do begin v:=L[i]; j:=i; while (j<>low(L))and(L[j-1]>v) do begin L[j]:=L[j-1]; j:=j-1; end; L[j]:=v; end; end; begin FillChar(RedArr,6,0); //clear for i:= 0 to ComponentCount-1 do if (Components[i] is TEdit) then (Components[i] as TEdit).Clear; //用随机数填充 for i:=1 to 6 do while true do begin b := false; value := MakeRandom(MaxRed); for j:=1 to i do if RedArr[j]=value then //已有重复数存在 begin b := false; break; end else b := true; if b then begin RedArr[i] := value; break; end; end; //排序 compositor(RedArr); //蓝球 while true do begin b := false; value := MakeRandom(MaxBlue); for i:=1 to 6 do if value=RedArr[i] then begin b := false; break; end else b := true; if b then break; end;