var Nums: array[0..9] of Byte; I, Idx: Integer; label L1; begin Randomize; Idx := Low(Nums); FillMemory(@Nums, Length(Nums), $FF); L1: Nums[Idx] := Random(100) + 1; for I := Low(Nums) to High(Nums) do begin if I = Idx then Continue; if Nums[I] = Nums[Idx] then begin Memo1.Text := '123'; goto L1; end; end; if Idx < High(Nums) then begin Inc(Idx); goto L1; end; end;
修正一下var Nums: array[0..9] of Byte; I, Idx: Integer; label L1; begin Randomize; Idx := Low(Nums); FillMemory(@Nums, Length(Nums), $FF); L1: Nums[Idx] := Random(101); for I := Low(Nums) to High(Nums) do begin if I = Idx then Continue; if Nums[I] = Nums[Idx] then goto L1; end; if Idx < High(Nums) then begin Inc(Idx); goto L1; end; end;
//CreateRandom(随机数取值范围起始值, 终止值; 返回数组长度) function TForm11.CreateRandom(const RangeStart, RangeEnd, ResultCount : integer) : Tarray<Integer>; var tmpLst : TStringList; //可以使用泛型列表或数组代替 I, n: Integer; begin if RangeStart >= RangeEnd then raise Exception.Create('错误的随机数范围!'); if ResultCount < 1 then raise Exception.Create('随机数返回数组长度必须大于0!'); tmpLst := TStringList.Create; tmpLst.Capacity := RangeEnd - RangeStart; try for n := RangeStart to RangeEnd do begin tmpLst.Add(IntToStr(n)); end; Randomize; SetLength(Result, ResultCount); for I := 0 to ResultCount do begin n := Random(tmpLst.Count - 1); Result[i] := StrToInt(tmpLst.Strings[n]); tmpLst.Delete(n); end; finally tmpLst.free; end; end;procedure TForm11.btn1Click(Sender: TObject); var MyArr : Tarray<Integer>; I : Integer; s : string; begin s := ''; MyArr := CreateRandom(0, 100, 10); for I := Low(MyArr) to High(MyArr) do s := s + inttostr(Myarr[i]) + ' '; ShowMessage(s); s := ''; MyArr := CreateRandom(100, 1000, 15); for I := Low(MyArr) to High(MyArr) do s := s + inttostr(Myarr[i]) + ' '; ShowMessage(s); end;
再定义一个数组来存放以前产生的数字,每次产生一个随机数就放进数组比较,有就另外再生成随机数;没有就放进数组。
Nums: array[0..9] of Byte;
I, Idx: Integer;
label
L1;
begin
Randomize;
Idx := Low(Nums);
FillMemory(@Nums, Length(Nums), $FF);
L1:
Nums[Idx] := Random(100) + 1;
for I := Low(Nums) to High(Nums) do
begin
if I = Idx then
Continue; if Nums[I] = Nums[Idx] then
begin
Memo1.Text := '123';
goto L1;
end;
end; if Idx < High(Nums) then
begin
Inc(Idx);
goto L1;
end;
end;
Nums: array[0..9] of Byte;
I, Idx: Integer;
label
L1;
begin
Randomize;
Idx := Low(Nums);
FillMemory(@Nums, Length(Nums), $FF);
L1:
Nums[Idx] := Random(101);
for I := Low(Nums) to High(Nums) do
begin
if I = Idx then
Continue; if Nums[I] = Nums[Idx] then
goto L1;
end; if Idx < High(Nums) then
begin
Inc(Idx);
goto L1;
end;
end;
function TForm11.CreateRandom(const RangeStart, RangeEnd, ResultCount : integer) : Tarray<Integer>;
var
tmpLst : TStringList; //可以使用泛型列表或数组代替
I, n: Integer;
begin
if RangeStart >= RangeEnd then
raise Exception.Create('错误的随机数范围!'); if ResultCount < 1 then
raise Exception.Create('随机数返回数组长度必须大于0!'); tmpLst := TStringList.Create;
tmpLst.Capacity := RangeEnd - RangeStart;
try
for n := RangeStart to RangeEnd do
begin
tmpLst.Add(IntToStr(n));
end; Randomize; SetLength(Result, ResultCount);
for I := 0 to ResultCount do
begin
n := Random(tmpLst.Count - 1);
Result[i] := StrToInt(tmpLst.Strings[n]);
tmpLst.Delete(n);
end;
finally
tmpLst.free;
end;
end;procedure TForm11.btn1Click(Sender: TObject);
var
MyArr : Tarray<Integer>;
I : Integer;
s : string;
begin
s := '';
MyArr := CreateRandom(0, 100, 10);
for I := Low(MyArr) to High(MyArr) do
s := s + inttostr(Myarr[i]) + ' '; ShowMessage(s); s := '';
MyArr := CreateRandom(100, 1000, 15);
for I := Low(MyArr) to High(MyArr) do
s := s + inttostr(Myarr[i]) + ' '; ShowMessage(s);
end;
取前10个单元
创建100个线程. 然后取线程的值..