我想从个数中随机取n个数,而且这n个数要不重复的。代码如下:procedure TForm1.ChoosenumClick(Sender: TObject);
var
m,n: integer;
i,k: integer;
t: array [1..30] of boolean;
begin
for k:=1 to 30 do
t[k]:=False;
n:=strtoint(Edit_num.Text);
if Label3.Caption<>'' then
Label3.Caption:=''; for i:=1 to n do
begin
m:=Random(strtoint(Edit_fanwei.Text));
if t[m]=True then
// i:=i-1
else
//n:=n+1;
begin
Label3.Caption:=Label3.Caption+' '+inttostr(m);
t[m]:=True;
end;
end; { repeat
m:=Random(strtoint(Edit_fanwei.Text));
if t[m]=True then
i:=i-1
else
begin
Label3.Caption:=Label3.Caption+' '+inttostr(m);
t[m]:=True;
i:=i+1;
end;
until(i>=n); }
end;用for循环就是当有重复时会浪费一次计算,从而随机数达不到n个
用repeat就是不知为什么老弹出内存错误
请问是什么问题?
var
m,n: integer;
i,k: integer;
t: array [1..30] of boolean;
begin
for k:=1 to 30 do
t[k]:=False;
n:=strtoint(Edit_num.Text);
if Label3.Caption<>'' then
Label3.Caption:=''; for i:=1 to n do
begin
m:=Random(strtoint(Edit_fanwei.Text));
if t[m]=True then
// i:=i-1
else
//n:=n+1;
begin
Label3.Caption:=Label3.Caption+' '+inttostr(m);
t[m]:=True;
end;
end; { repeat
m:=Random(strtoint(Edit_fanwei.Text));
if t[m]=True then
i:=i-1
else
begin
Label3.Caption:=Label3.Caption+' '+inttostr(m);
t[m]:=True;
i:=i+1;
end;
until(i>=n); }
end;用for循环就是当有重复时会浪费一次计算,从而随机数达不到n个
用repeat就是不知为什么老弹出内存错误
请问是什么问题?
解决方案 »
- 在线程中动态创建ADO,执行SQL语句,执行函数时出问题
- 求ShowModal转Show的方法
- 现场80分,我用frxPreView做预览窗口,怎么用语句实现一些功能。
- 关于xml的问题,在线等!
- 不打开word,如何浏览word文件内容,用OLE太慢!
- 急急,使用TQuickRep做报表时,自定义打印宽度和高度,为什么不打印呢
- ^O~ 如夢领分。
- DELPHI初学者忘各位老师指教:ADO实现保存数据到access数据库
- 请问怎样取得Excel表的行数和列数?谢谢
- 如何把一个表的一条纪录,新增加到另一表中?
- DELPHI IN 用法
- 请问如何post数据,数据包已经截下来,麻烦高手写个代码,谢谢了
还有,在程序开始时对i赋初值0
还有 i:=i-1 这句是不用的,不然产生的数字个数会多出来
m:=Random(strtoint(Edit_fanwei.Text));
if t[m]=True then
//i:=i-1
else
begin
Label3.Caption:=Label3.Caption+' '+inttostr(m);
t[m]:=True;
i:=i+1;
end;
until(i>=n);
但是感觉这样还是不对。当m>k时,数组就溢出了,再就是m<n时(即输入的Edit_fanwei.Text<Edit_num.Text)时,i最大就只能是m,那也跳不出循环
var
i,j: integer;
vstrList: TStringList;
begin
Memo1.Lines.Clear;
vstrList := TStringList.Create;
try
i := 0;
while i<>StrToInt(Edit1.Text) do //当取出N个数时就跳出循环
begin
Randomize;
j := Random(StrToInt(Edit2.Text));
while true do
begin
if vstrlist.IndexOf(inttostr(j))=-1 then //如果不重复就跳出
begin
vstrList.Add(inttostr(j));
i := i+ 1;
break;
end;
end;
end;
memo1.Lines.Add(vStrList.Text);
finally
vstrlist.Free;
end;
end;
var
i,j: integer;
vstrList: TStringList;
begin
Memo1.Lines.Clear;
vstrList := TStringList.Create;
try
i := 0;
while i<>StrToInt(Edit1.Text) do //当取出N个数时就跳出循环
begin
Randomize;
j := Random(StrToInt(Edit2.Text));
while true do
begin
if vstrlist.IndexOf(inttostr(j))=-1 then //如果不重复就跳出
begin
vstrList.Add(inttostr(j));
i := i+ 1;
break;
end;
j := Random(StrToInt(Edit2.Text));
end;
end;
memo1.Lines.Add(vStrList.Text);
finally
vstrlist.Free;
end;
end;
i,j:integer;
str:string;
swapa:string[2];
aa:array[1..36] of string[2];
begin
for i:=1 to 36 do
aa[i]:=inttostr(i);
randomize;
for i:=1 to 36 do
begin
j:=1+random(36);
swapa:=aa[i];
aa[i]:=aa[j];
aa[j]:=swapa;
end;
for i:=1 to 36 do
str:=str+aa[i]+':';
showmessage(str);
end;36个的随机数
就是随机数多就窗体变大,随机数个数少窗体就变小。
还有在panel里怎么换行啊?
procedure TForm1.Button1Click(Sender: TObject);
var
A, B: array of Integer;
n, m, k: Integer;
I: Integer;
begin
n := StrToInt(Edit1.Text);
m := StrToInt(Edit2.Text);
//从N个数中随机抽取M个非重复的数
if (m > 0) and (m < n) then
begin
SetLength(A, n);
//初始化
for I := 0 to n - 1 do //如果不包括 0 就从 1 开始循环到 n
A[I] := I; SetLength(B, m);
Randomize;
//从数组 A 中以随机的位置抽取
while m >= 0 do
begin
K := Random(n);
B[m] := A[K];
A[K] := A[n - 1]; //将抽取的数移动到当前数据的后面
Dec(n); //缩小数组 A 以便去掉刚刚抽取的那个数
Dec(m);
end; //显示一下
Memo1.Lines.Clear;
for I := Low(B) to high(B) do
Memo1.Lines.Add(IntToStr(B[I]))
end;
A[K] := A[n - 1]; //将抽取的数移动到当前数据的后面
应该是
A[K] := A[n - 1]; //将当前大小的数组最后面的数移动到被抽取的数的位置