Perl | srand() Function

The srand() function in Perl helps rand() function to generate a constant value each time the program is run.
This srand() function uses the same parameter each time the rand() function is called for getting constant random value.

Syntax: srand(seed)

Parameters:
seed : It is an integer value.



Return: This function does not return any value.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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");

chevron_right



Output:

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.

Note: In the above code, it can be seen that the parameter of srand() function is same that is why the rand() function produced the same random value each time it is called.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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");

chevron_right


Output :

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.

Note: In the above code, it can be seen that the srand() functions are having a different parameter that is why the rand() function produced the different random value each time it is called.

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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");

chevron_right


Output :

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.

Note: In the above code, it can be seen that the srand() functions are not used that is why the rand() function produced the different random value each time it is called.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.