implementation var bRun: Boolean; //运行标志 {$R *.dfm}procedure TForm1.FormCreate(Sender: TObject); begin bRun := False; AdoQuery1.Close; AdoQuery1.SQL.Clear; AdoQuery1.SQL.Add('select top 1 * from jobs order by newid()'); AdoQuery1.Open; end;procedure TForm1.Button1Click(Sender: TObject); begin bRun := not bRun; while bRun do begin AdoQuery1.Active := true; Edit1.text := AdoQuery1.FieldByName('job_id').AsString; Application.ProcessMessages; sleep(50); AdoQuery1.Active := False; end; end;
这段代码有不合理的地方吗? procedure TForm1.BigerCoverClick(Sender: TObject); var i, AllMember: integer; tmpStringList: TStringList; SmallTmpList: TStringList; begin //获得临时中奖列表 theStringList := TStringList.Create(); SmallTmpList := TStringList.Create(); theStringList.LoadFromFile(tmpFileName); //获得中奖人个数 AllMember := theStringList.Count; if RadioButton1.Checked or RadioButton2.Checked or RadioButton3.Checked or RadioButton4.Checked then //抽奖项目开始 begin if AllMember > 0 then begin if GoGoFlag = 'GO!' then begin GoGoFlag := 'STOP!'; LabelArea.Font.Color := clblack; end else begin //更新当前的中奖人 NowGiftsIndex := NowGiftsIndex + 1; GoGoFlag := 'GO!'; //---填写中奖人信息开始--- SmallTmpList := split(theStringList[ArrayIndex], ','); if (theStringList[ArrayIndex] <> '') and (SmallTmpList.Count = 7) then begin //如果重复就不更新LabelArea if LabelArea.Caption <> SmallTmpList[4] then LabelArea.Caption := SmallTmpList[4]; end else begin //如果获奖人为空,在此循环 BigerCoverClick(nil); end; //中奖人颜色变红色 LabelArea.Font.Color := clRed; //---填写中奖人信息结束--- if RadioButton1.Checked then begin Memo2.Lines.Add('特等奖:' + Label1.Caption) ; RadioButton1.Checked := false; RadioButton1.Enabled := false; end;if RadioButton2.Checked then begin j := j + 1; if j < 6 then Memo2.Lines.Add('一等奖:' + Label1.Caption ) else begin RadioButton2.Checked := false; RadioButton2.Enabled := false; showmessage('一等奖5名,号码抽取结束'); end; end; if RadioButton3.Checked then begin k := k + 1; if k < 21 then Memo2.Lines.Add('二等奖:' + Label1.Caption) else begin RadioButton3.Checked := false; RadioButton3.Enabled := false; showmessage('二等奖20名,号码抽取结束'); end; end; if RadioButton4.Checked then begin str1 := Copy(Label1.Caption, length(Label1.Caption) - 2, 3); ADOQuery1.close; ADOQuery1.sql.clear; ADOQuery1.sql.Add('select count(*) from etmp where 号码 like "%"+:str1'); ADOQuery1.Parameters.ParamByName('str1').value := str1; ADOQuery1.open; n := ADOQuery1.fields[0].asinteger; l := l + n; if l <1000 then begin Memo2.Lines.Add('三等奖尾数为:' + str1); end else begin RadioButton4.Checked := false; RadioButton4.Enabled := false; showmessage('已经抽取完毕'); end; end; //---预备下次抽奖文件开始--- //排除本次中奖人 tmpStringList := TStringList.Create(); for i := 0 to AllMember - 1 do begin if (i <> ArrayIndex) and (theStringList[i] <> '') then begin tmpStringList.Add(theStringList[i]); end; end; //删除上次抽奖文件 DeleteFile(tmpFileName); //保存下次抽奖文件 tmpStringList.savetofile(tmpFileName); tmpStringList.Free; //---预备下次抽奖文件结束--- end; //---中奖列表循环开始-------------------- SmallTmpList := TStringList.Create(); while GoGoFlag = 'STOP!' do begin //--更新随机种子数 Randomize; //--获得当前随机数 ArrayIndex := RandomRange(0, AllMember); //--获得的获奖人不能为空 if theStringList[ArrayIndex] <> '' then begin SmallTmpList := split(theStringList[ArrayIndex], ','); if LabelArea.Caption <> SmallTmpList[4] then LabelArea.Caption := SmallTmpList[4]; Application.HandleMessage; sleep(10); end; end; //---中奖列表循环开始-------------------- end; SmallTmpList.Free; end; //抽奖项目结束 end;end.
function rand(recordcount:integer):integer; var i:integer; begin for i:=0 to 100 do begin form1.edit1.text:=inttostr(floor(random(recordcount))); //当然你要用dataset.recno来显示真正的号码 form1.Edit1.Refresh; sleep(100); end;end; 用了一下,效果很好哟,只用在form上加一个edit1,再用一个button来运行 procedure TForm1.Button1Click(Sender: TObject); begin rand(10000); end; 就可以试着看一下了。
var
bRun: Boolean; //运行标志
{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
bRun := False;
AdoQuery1.Close;
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add('select top 1 * from jobs order by newid()');
AdoQuery1.Open;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
bRun := not bRun;
while bRun do
begin
AdoQuery1.Active := true;
Edit1.text := AdoQuery1.FieldByName('job_id').AsString;
Application.ProcessMessages;
sleep(50);
AdoQuery1.Active := False;
end;
end;
procedure TForm1.BigerCoverClick(Sender: TObject);
var
i, AllMember: integer;
tmpStringList: TStringList;
SmallTmpList: TStringList;
begin
//获得临时中奖列表
theStringList := TStringList.Create();
SmallTmpList := TStringList.Create();
theStringList.LoadFromFile(tmpFileName);
//获得中奖人个数
AllMember := theStringList.Count;
if RadioButton1.Checked or RadioButton2.Checked or RadioButton3.Checked or
RadioButton4.Checked then //抽奖项目开始
begin
if AllMember > 0 then
begin
if GoGoFlag = 'GO!' then
begin
GoGoFlag := 'STOP!';
LabelArea.Font.Color := clblack;
end
else
begin
//更新当前的中奖人
NowGiftsIndex := NowGiftsIndex + 1;
GoGoFlag := 'GO!';
//---填写中奖人信息开始---
SmallTmpList := split(theStringList[ArrayIndex], ',');
if (theStringList[ArrayIndex] <> '') and (SmallTmpList.Count = 7) then
begin
//如果重复就不更新LabelArea
if LabelArea.Caption <> SmallTmpList[4] then
LabelArea.Caption := SmallTmpList[4];
end
else
begin
//如果获奖人为空,在此循环
BigerCoverClick(nil);
end; //中奖人颜色变红色
LabelArea.Font.Color := clRed;
//---填写中奖人信息结束--- if RadioButton1.Checked then
begin
Memo2.Lines.Add('特等奖:' + Label1.Caption) ;
RadioButton1.Checked := false;
RadioButton1.Enabled := false;
end;if RadioButton2.Checked then
begin
j := j + 1;
if j < 6 then
Memo2.Lines.Add('一等奖:' + Label1.Caption )
else
begin
RadioButton2.Checked := false;
RadioButton2.Enabled := false;
showmessage('一等奖5名,号码抽取结束');
end;
end; if RadioButton3.Checked then
begin
k := k + 1;
if k < 21 then
Memo2.Lines.Add('二等奖:' + Label1.Caption)
else
begin
RadioButton3.Checked := false;
RadioButton3.Enabled := false;
showmessage('二等奖20名,号码抽取结束');
end;
end; if RadioButton4.Checked then
begin
str1 := Copy(Label1.Caption, length(Label1.Caption) - 2, 3);
ADOQuery1.close;
ADOQuery1.sql.clear;
ADOQuery1.sql.Add('select count(*) from etmp where 号码 like "%"+:str1');
ADOQuery1.Parameters.ParamByName('str1').value := str1;
ADOQuery1.open;
n := ADOQuery1.fields[0].asinteger;
l := l + n;
if l <1000 then
begin
Memo2.Lines.Add('三等奖尾数为:' + str1);
end
else
begin
RadioButton4.Checked := false;
RadioButton4.Enabled := false;
showmessage('已经抽取完毕');
end;
end; //---预备下次抽奖文件开始---
//排除本次中奖人
tmpStringList := TStringList.Create();
for i := 0 to AllMember - 1 do
begin
if (i <> ArrayIndex) and (theStringList[i] <> '') then
begin
tmpStringList.Add(theStringList[i]);
end;
end;
//删除上次抽奖文件
DeleteFile(tmpFileName);
//保存下次抽奖文件
tmpStringList.savetofile(tmpFileName);
tmpStringList.Free;
//---预备下次抽奖文件结束--- end;
//---中奖列表循环开始--------------------
SmallTmpList := TStringList.Create();
while GoGoFlag = 'STOP!' do
begin
//--更新随机种子数
Randomize;
//--获得当前随机数
ArrayIndex := RandomRange(0, AllMember);
//--获得的获奖人不能为空
if theStringList[ArrayIndex] <> '' then
begin
SmallTmpList := split(theStringList[ArrayIndex], ',');
if LabelArea.Caption <> SmallTmpList[4] then
LabelArea.Caption := SmallTmpList[4];
Application.HandleMessage;
sleep(10);
end;
end;
//---中奖列表循环开始--------------------
end;
SmallTmpList.Free; end; //抽奖项目结束
end;end.
你的方法,速度好慢呀,有其他方法吗?http://community.csdn.net/Expert/topic/3394/3394725.xml?temp=.8783075
var i:integer;
begin
for i:=0 to 100 do
begin
form1.edit1.text:=inttostr(floor(random(recordcount))); //当然你要用dataset.recno来显示真正的号码
form1.Edit1.Refresh;
sleep(100);
end;end;
用了一下,效果很好哟,只用在form上加一个edit1,再用一个button来运行
procedure TForm1.Button1Click(Sender: TObject);
begin
rand(10000);
end;
就可以试着看一下了。
1。你可以将文件中的数据导入数据库
可参考http://community.csdn.net/Expert/topic/3278/3278767.xml?temp=.3872034
2。用sql语句直接在数据库中随机抽取