简体   繁体   English

Delphi7,Randomize,从1到6中选择随机数,但不是0

[英]Delphi7, Randomize, choose random numbers from 1 to 6, but not 0

i'd like to make 2 dices, but i don't want it to choose 0, this is my code: 我想制作2个骰子,但我不希望它选择0,这是我的代码:

procedure TForm1.Button1Click(Sender: TObject);
var x1,x2:integer; text1,text2:string;
begin
randomize;
x1:=random(7);
x2:=random(7);

text1:=inttostr(x1);
text2:=inttostr(x2);

label1.Caption:=text1;
label2.Caption:=text2;

end;
end.

What should i do to make it choose from 1 to 6, without including 0? 我该怎么办才能让它从1到6中选择,不包括0? thanks 谢谢

x1:=random(6) + 1;

应该做的伎俩,它现在永远不会返回零。

Use RandomRange : 使用RandomRange

uses Math;

begin
  x1 := RandomRange(1, 7);

(Which does internally exactly the same as Chris' answer ...) (内部与克里斯的回答完全相同......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM