★★★关于随机数:求randomize random的用法及注意事项及原理及其它的随机方法?我给分。谢谢(20分)

B

bcfans

Unregistered / Unconfirmed
GUEST, unregistred user!
★★★关于随机数:求randomize random的用法及注意事项及原理及其它的随机方法?我给分。谢谢
 
第一次使用 random 前调用一下 randomize ,以后可以不调用randomize
如果不调用randomize,程序每次执行产生的随机数序列是一样的。
 
我给你写了一个例子。不知是否有用。
var
Form1: TForm1;


implementation

{$R *.DFM}
var
stepleft:integer;
steptop:integer;
stepleft1:integer;
steptop1:integer;
clicked:boolean;
flag:boolean;
i:integer;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
i:=0;
edit1.Text:=inttostr(i)+' 次';
stepleft1:=10;
steptop1:=15;
stepleft:=10;
steptop:=15;
clicked:=true;
button1.Caption:='运 行';
timer1.Enabled:=false;
randomize;//在调用RANDOM之前,使用RANDOMIZE初始化
shape1.Left:=random(panel1.Width-40);//在0到PANEL1.WIDTH之间产生任意的随机整数
shape1.Top:=random(panel1.Height-40);
shape2.Left:=random(panel1.Width-40);
shape2.Top:=random(panel1.Height-40);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if clicked = true then
begin
timer1.Enabled:=true;
clicked := false;
button1.Caption:='中 止';
end
else
begin
timer1.Enabled:=false;
clicked := true;
button1.Caption:='运 行';
end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
x,y,x1,y1:integer;
begin
x:=shape1.Left+20;
y:=shape1.Top+20;
x1:=shape2.Left+20;
y1:=shape2.Top+20;

if (sqrt(sqr(abs((x1-x)))+sqr(abs((y1-y)))) <=40) and (flag = false) then
begin
stepleft:=-stepleft;
steptop:=steptop;
stepleft1:=-stepleft1;
steptop1:=steptop1;
image1.Picture.LoadFromFile('D:/Program Files/Common Files/Borland Shared/Images/Splash/16Color/skyline.bmp');
flag:=true;
i:=i+1;
edit1.Text:=inttostr(i)+' 次';
end
else
if (sqrt(sqr(abs((x1-x)))+sqr(abs((y1-y)))) <=40) and (flag = true) then
begin
stepleft:=-stepleft;
steptop:=steptop;
stepleft1:=-stepleft1;
steptop1:=steptop1;
image1.Picture.LoadFromFile('D:/Program Files/Common Files/Borland Shared/Images/Splash/16Color/athena.bmp');
flag:=false;
i:=i+1;
edit1.Text:=inttostr(i)+' 次';
end;
shape1.Left:=shape1.Left+stepleft;
shape1.Top:=shape1.Top+steptop;
shape2.Left:=shape2.Left+stepleft1;
shape2.Top:=shape2.Top+steptop1;
if (shape1.Left>=panel1.Width-40) or (shape1.Left<=0)then
stepleft:=-stepleft;
if (shape1.top>=panel1.Height-40) or (shape1.top<=0)then
steptop:=-steptop;
if (shape2.Left>=panel1.Width-40) or (shape2.Left<=0)then
stepleft1:=-stepleft1;
if (shape2.top>=panel1.Height-40) or (shape2.top<=0)then
steptop1:=-steptop1;

end;

end.
 
Random函数有两种用法,带参数Random(range)和不带参数,带正整数参数Range时,返回
一个大于或等于0,小于Range的随机整数,如不带参数,则返回一个0与1之间(0<=X<1)的
随机实数。第一次使用前要初始化,调用Randomize。
 
比我理解的好!楼上!
 
多人接受答案了。
 
顶部