珀尔 | rand()函数
Perl 中的 rand()函数返回一个介于 0 和传递给它的正数值之间的随机小数,如果没有指定值,则返回 1。自动调用 srand() 来播种随机数生成器,除非它已经被调用。
Syntax: rand(range_value)
Parameter:
range_value: a positive number to specify the range
Returns:
a random Floating point number between 0 and the specified value
示例 1:
#!/usr/bin/perl -w
# Calling the rand function
$random = rand(15);
# print the random number
# between 0 and 15
print("Random Number between 0 and 15: ",
$random, "\n");
输出:
Random Number between 0 and 15: 12.6694798049858
示例 2:
#!/usr/bin/perl -w
# Calling the rand function
# with negative value
$random = rand(-25);
# print the random number
# between 0 and -25
print("Random Number between 0 and -25: ",
$random, "\n");
输出:
Random Number between 0 and -25: -7.54296459674615