procedure TForm1.Timer1Timer(Sender: TObject); begin Randomize; //更新随机种子 with TStringList.Create do try if not FileExists('c:\temp.txt') then Exit; //如果没有就退出 LoadFromFile('c:\temp.txt'); //载入文本文件~~ if Count <= 0 then Exit; //如果文件为空就退出 edit3.text:=Strings[Random(Count)]; edit4.text:=Strings[Random(Count)]; finally Free; end; end;procedure TForm1.GroupBox1Click(Sender: TObject); begin if Timer1.Enabled then Timer1.Enabled:=false else Timer1.Enabled:=true; end; ******************************************************** 如何防止EDIT3.TEXT与EDIT4.TEXT抽出的号码一样?
procedure TForm1.Button1Click(Sender: TObject); var i:Integer; begin Memo1.Lines.Clear ; for i:=0 to 4999 do begin //产生一个0至8000的随机数 a[i]:=Random(8001); Application.ProcessMessages(); Memo1.Lines.Add(IntToStr(a[i])); end; for i:=0 to 4999 do b[i]:=a[i]; Button1.Enabled:=False; Button2.Enabled:=True; end;
把号码存入数组进行编号,Random出编号就可以了
with TStringList.Create do try
if not FileExists('c:\temp.txt') then Exit; //安全第一~~
LoadFromFile('c:\temp.txt'); //载入文本文件~~
if Count <= 0 then Exit; //安全第一~~
ShowMessage(Strings[Random(Count)]); //就是它~~
{ TODO -oZswang - c2002.10.23 : 如果是需要抽出多个人,只需要把抽出的人从列表中删除在用该方法抽取即可 }
finally
Free;
end;请掌握如下方法和VCL类:
Randomize();
Random();
TStrings,TStringList;另外可以去搜索“随机 抽取 元素 算法 delphi”等资料和例子
begin
Randomize; //更新随机种子
with TStringList.Create do try
if not FileExists('c:\temp.txt') then Exit; //如果没有就退出
LoadFromFile('c:\temp.txt'); //载入文本文件~~
if Count <= 0 then Exit; //如果文件为空就退出
edit3.text:=Strings[Random(Count)];
edit4.text:=Strings[Random(Count)];
finally
Free;
end;
end;procedure TForm1.GroupBox1Click(Sender: TObject);
begin
if Timer1.Enabled then
Timer1.Enabled:=false
else
Timer1.Enabled:=true;
end;
********************************************************
如何防止EDIT3.TEXT与EDIT4.TEXT抽出的号码一样?
var
i:Integer;
begin
Memo1.Lines.Clear ;
for i:=0 to 4999 do
begin
//产生一个0至8000的随机数
a[i]:=Random(8001);
Application.ProcessMessages();
Memo1.Lines.Add(IntToStr(a[i]));
end;
for i:=0 to 4999 do
b[i]:=a[i];
Button1.Enabled:=False;
Button2.Enabled:=True;
end;
随即选取第n条记录即可