J
jun_happy
Unregistered / Unconfirmed
GUEST, unregistred user!
问题:随机问题,(急) ( 积分:100, 回复:5, 阅读:34 )
分类:数据结构 ( 版主:LeeChange, 张剑波 )
来自:jun_happy, 时间:2004-5-21 17:57:00, ID:2621375 | 编辑 [显示:小字体 | 大字体]
我想从20个学生中随机抽取5个学生,该怎么实现啊,先谢了
来自:xuhao1, 时间:2004-5-21 18:10:52, ID:2621396
Randomize;
//初始化
RandomRange(1, 20);
//随即产出1到20的数
来自:TYZhang, 时间:2004-5-21 18:20:35, ID:2621410
procedure TForm1.Button4Click(Sender: TObject);
var L1,L2:TList;
I,K:Integer;
begin
L1:=TList.Create;
L2:=TList.Create;
for I:=1 to 20do
L1.Add(Pointer(I));
Randomize;
for I:=1 to 5do
begin
K:=Random(L1.Count-1);
L2.Add(L1[K]);
L1.Delete(K);
end;
for I:=0 to 4do
ShowMessage(IntToStr(Integer(L2)));
L1.Free;
L2.Free;
end;
来自:jun_happy, 时间:2004-5-21 20:31:28, ID:2621540 | 编辑
能不能使用如下方式:
st:=query1.recordcount;
sel:=10;
f:=1;
while(f<sel)do
begin
num:=random(st)+1;
query1.first;
query1.moveby(num-1);
...
f:=f+1;
end;
但这样做会产生相同的记录,特别是sel和st接近时,怎样才能不会有相同的记录呢?
请解决!
来自:TYZhang, 时间:2004-5-21 20:48:01, ID:2621560
procedure TForm1.Button4Click(Sender: TObject);
var L1:TList;
I,num,sel:Integer;
begin
L1:=TList.Create;
try
for I:=0 to query1.recordcount-1do
L1.Add(Pointer(I));
sel:=10;
Randomize;
for I:=1 to seldo
begin
num:=Random(L1.Count-1);
query1.first;
query1.moveby(num);
L1.Delete(num);
...........
end;
finally
L1.Free;
end;
end;
分类:数据结构 ( 版主:LeeChange, 张剑波 )
来自:jun_happy, 时间:2004-5-21 17:57:00, ID:2621375 | 编辑 [显示:小字体 | 大字体]
我想从20个学生中随机抽取5个学生,该怎么实现啊,先谢了
来自:xuhao1, 时间:2004-5-21 18:10:52, ID:2621396
Randomize;
//初始化
RandomRange(1, 20);
//随即产出1到20的数
来自:TYZhang, 时间:2004-5-21 18:20:35, ID:2621410
procedure TForm1.Button4Click(Sender: TObject);
var L1,L2:TList;
I,K:Integer;
begin
L1:=TList.Create;
L2:=TList.Create;
for I:=1 to 20do
L1.Add(Pointer(I));
Randomize;
for I:=1 to 5do
begin
K:=Random(L1.Count-1);
L2.Add(L1[K]);
L1.Delete(K);
end;
for I:=0 to 4do
ShowMessage(IntToStr(Integer(L2)));
L1.Free;
L2.Free;
end;
来自:jun_happy, 时间:2004-5-21 20:31:28, ID:2621540 | 编辑
能不能使用如下方式:
st:=query1.recordcount;
sel:=10;
f:=1;
while(f<sel)do
begin
num:=random(st)+1;
query1.first;
query1.moveby(num-1);
...
f:=f+1;
end;
但这样做会产生相同的记录,特别是sel和st接近时,怎样才能不会有相同的记录呢?
请解决!
来自:TYZhang, 时间:2004-5-21 20:48:01, ID:2621560
procedure TForm1.Button4Click(Sender: TObject);
var L1:TList;
I,num,sel:Integer;
begin
L1:=TList.Create;
try
for I:=0 to query1.recordcount-1do
L1.Add(Pointer(I));
sel:=10;
Randomize;
for I:=1 to seldo
begin
num:=Random(L1.Count-1);
query1.first;
query1.moveby(num);
L1.Delete(num);
...........
end;
finally
L1.Free;
end;
end;