How can I create a random number between two numbers in Perl

独自空忆成欢 提交于 2021-01-21 06:46:07

问题


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;

Then to create a random integer between those two. (whole number) ($random_num)

I've looked into this site: http://perlmeme.org/howtos/perlfunc/rand_function.html

Which is a good resource, although does not do exactly that.


回答1:


my $x = $minimum + int(rand($maximum - $minimum));

http://perldoc.perl.org/functions/rand.html

Note this range excludes $maximum itself. Add 1 to make it inclusive.




回答2:


my $random_num = int($minimum + rand($maximum - $minimum));


来源:https://stackoverflow.com/questions/21806362/how-can-i-create-a-random-number-between-two-numbers-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!