📜  珀尔 | srand()函数

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

珀尔 | srand()函数

Perl 中的 srand()函数帮助 rand()函数在程序每次运行时生成一个常数值。
每次调用 rand()函数以获取恒定随机值时,此 srand()函数都使用相同的参数。

示例 1:

#!/usr/bin/perl
  
# Calling the srand() function and 
# Printing a random value 
srand(5);
print("The first random number is ", rand(), ".\n");
  
# Calling the srand() function and 
# Printing a random value 
srand(5);
print("The second random number is ", rand(), ".\n");
  
# Calling the srand() function and 
# Printing a random value 
srand(5);
print("The third random number is ", rand(), ".\n");
  
# Calling the srand() function and 
# Printing a random value 
srand(5);
print("The fourth random number is ", rand(), ".\n");


输出:

The first random number is 0.524839579434232.
The second random number is 0.524839579434232.
The third random number is 0.524839579434232.
The fourth random number is 0.524839579434232.

注意:在上面的代码中,可以看出 srand()函数的参数是相同的,这就是为什么每次调用 rand()函数都会产生相同的随机值。

示例 2:

#!/usr/bin/perl
  
# Calling the srand() function and 
# Printing a random value 
srand(5);
print("The first random number is ", rand(), ".\n");
  
# Calling the srand() function and 
# Printing a random value 
srand(6);
print("The second random number is ", rand(), ".\n");
  
# Calling the srand() function and 
# Printing a random value 
srand(7);
print("The third random number is ", rand(), ".\n");
  
# Calling the srand() function and 
# Printing a random value 
srand(8);
print("The fourth random number is ", rand(), ".\n");

输出 :

The first random number is 0.524839579434232.
The second random number is 0.395641888099821.
The third random number is 0.266444196765409.
The fourth random number is 0.137246505430998.

注意:在上面的代码中,可以看出 srand() 函数具有不同的参数,这就是为什么 rand()函数每次调用时都会产生不同的随机值。

示例 3:

#!/usr/bin/perl
  
# Printing a random value without calling the srand() function
print("The first random number is ", rand(), ".\n");
  
# Printing a random value without calling the srand() function
print("The second random number is ", rand(), ".\n");
  
# Printing a random value without calling the srand() function
print("The third random number is ", rand(), ".\n");
  
# Printing a random value without calling the srand() function
print("The fourth random number is ", rand(), ".\n");

输出 :

The first random number is 0.241482914266275.
The second random number is 0.821264154368208.
The third random number is 0.870625858233449.
The fourth random number is 0.012084407123389.

注意:在上面的代码中,可以看出没有使用 srand() 函数,这就是为什么每次调用 rand()函数都会产生不同的随机值。