这是在Delphi中(确切地说是7版本)。如何在特定范围内生成随机数?类似于Python中的random.randint(1,6)
。我正在尝试模拟掷骰子。另一个选择是以某种方式排除0。
目前我有:
Randomize;
Roll := Random(7);
Label3.Caption := IntToStr(Roll);
你可以使用
RandomRange(1, 7)
该函数将从集合 {1, 2, 3, 4, 5, 6} 中返回一个随机整数。
(使用 Math 库
)
[顺便提一下,要排除零非常简单。只需执行 Random(6) + 1
即可。]
r := random;
x := (b-a)*r + a;
r := RandomRange(0, N-1);
x := a + r*(b-a)/(N-1);
RandomRange(0, N)
。 - David Heffernan
randomize;
ran:=random(6)+1;
Random(6) + 1
即可。 - Andreas Rejbrand