Syntax:
The syntax of the rand() function is shown in the following. It can be used without any argument and with a numeric argument. When this function is used without any argument, the fractional number within 0 to 1 is generated. When this function is used with an argument value, a random number within 0 to the argument value is generated.
Or
Different Examples of the Perl Rand() Function
The uses of the rand() function without and with the argument value are shown in this part of the tutorial.
Example 1: Generate a Fractional Random Number
Create a Perl file with the following script that generates a fractional random number between 0 to 1. The generated number is printed later.
use strict;
use warnings;
use 5.34.0;
#Call a random number without argument
my $r_number = rand();
#Print the generated random number
say "The generated random number is $r_number";
Output:
The following similar output appears if you execute the script two times. Different fractional numbers are generated in different executions.
Example 2: Generate a Random Number between 0 to 50
The random number of specific ranges can be generated using the rand() function with the argument value. Create a Perl file with the following script that generates a random number between 0 to 50. Here, the maximum value of the random number is defined in the argument value of the rand() function. The int() function is used in the script to convert the fractional value that is generated from the rand() function into the integer value.
Output:
The following similar output appears if you execute the script two times. Twelve (12) is generated in the first execution of the script and nine (9) is generated in the second execution of the script. Both numbers are within the range of 0 to 50:
Example 3: Generate a Random Number within 100 to 150
In the previous two examples, the minimum value of the random number was 0 by default. But if you want, you can set the minimum value with the maximum value while generating the random number. Create a Perl file with the following script that generates a random number within 100 to 150 using the rand() function. Here, the $range variable is used to set the maximum value of the random number that is generated by adding the minimum value of 100 with this variable. Like in the previous example, the int() function is used to convert the fractional number to an integer number.
use strict;
use warnings;
use 5.34.0;
#Define the range of the random number
my $range = 50;
#Define the minimum value of the random number
my $min = 100;
#Generate the random number of the specified range
my $r_number = int(rand($range)) + $min;
#Print the random number
say $r_number;
Output:
The following similar output appears if you execute the script two times. One-hundred nineteen (119) is generated in the first execution of the script where the generated random number is 19 and 100+19 is 119. One-hundred four (104) is generated in the second execution of the script where the generated random number is 4 and 100+4 is 104. Both numbers are within the range of 100 to 150:
Example 4: Generate a Random Number Based on the Input Value
Create a Perl file with the following script that generates an integer random number based on the user’s input. The numeric value that is taken from the user is used as the argument value of the rand() function. So, a random number from 0 to the input value is generated after executing the script.
use strict;
use warnings;
use 5.34.0;
print "Enter the range of the random number: ";
#Take the range value from the user
my $number = ;
#Remove the newline
chomp($number);
#Generate the random number
my $r_number = int(rand($number));
#Print the generated random number
say "Generated random Number within 0 and $number is $r_number";
Output:
The following similar output appears if you execute the script two times. In the first execution, 45 is taken as the input value and 26 is generated as the random number. In the second execution, -80 is taken as the input and -25 is generated as the random number:
Conclusion
The methods of generating a random number of different ranges are shown in this tutorial using multiple examples.