📜  珀尔 | rand()函数

📅  最后修改于: 2022-05-13 01:55:19.712000             🧑  作者: Mango

珀尔 | rand()函数

Perl 中的 rand()函数返回一个介于 0 和传递给它的正数值之间的随机小数,如果没有指定值,则返回 1。自动调用 srand() 来播种随机数生成器,除非它已经被调用。

示例 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