随即生成31以内的数(体彩)delphi

来源:百度知道 编辑:UC知道 时间:2024/05/15 07:58:46
生成31以内的7个数 显示出来 不能重复的 怎么做?

我的百度空间里有一题:http://hi.baidu.com/hxh147
有代码也有截图,你可以去看看!

自己写了一个函数,蛮实用的。

function UnSameRand(x, y: Integer):TStringList;
var //x:总数,y:范围
i,count,z:Integer;
str:TStringList;
a:array of Integer;
label l;
begin
if (x>=y) or (x=0) or (y=0) then
begin
Application.MessageBox('对不起,随机数总数和范围均不能为零、不能相等且范围必须要大于总数。','提示',0+16+0);
Result:=nil;
Exit;
end
else begin
str:=TStringList.Create;
Randomize;
SetLength(a,x);
for i:=0 to x-1 do
begin
l:z:=Random(y);
for count:=0 to i do
begin
if a[count]=z then
begin
goto l;
end;
end;
a[i]:=z;
str.Add(IntToStr(z));
end;
Result:=str;
end;