如何在 Perl 中的两个数字之间创建一个随机数 [英] How can I create a random number between two numbers in Perl
本文介绍了如何在 Perl 中的两个数字之间创建一个随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在其他编程语言中看到了许多其他方法来做到这一点......虽然我还没有在 Perl 中找到一种......
I've seen many other ways to do this in other programming languages... Though I havent found one in Perl yet...
我想要完成的是设置两个数字:
What I want to accomplish is to set two numbers:
$minimum = 100;
$maximum = 4000;
然后在这两者之间创建一个随机整数.(完整的号码)($random_num)
Then to create a random integer between those two. (whole number) ($random_num)
我查看了这个网站:http://perlmeme.org/howtos/perlfunc/rand_function.html
这是一个很好的资源,虽然不完全是这样.
Which is a good resource, although does not do exactly that.
推荐答案
my $x = $minimum + int(rand($maximum - $minimum));
http://perldoc.perl.org/functions/rand.html
注意这个范围排除 $maximum
本身.添加 1 以使其包含在内.
Note this range excludes $maximum
itself. Add 1 to make it inclusive.
这篇关于如何在 Perl 中的两个数字之间创建一个随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文