 php

# Generate a random number in PHP

Generating a different number every time by executing the script is called the random number. The random number can be used for various purposes in the programming, such as generating a random filename, random password, and a random number that is not predictable to others. PHP has many built-in functions to generate random numbers in different ways. rand(), random_int(), and mt_rand() functions are used in PHP to generate random numbers. How these functions is used to generate the random numbers are explained in this tutorial.

## Use of rand()

This function is used to generate a random integer number. The syntax of this function is given below.

Syntax:

int rand()

or

int rand(int min, int max)

It returns a large random integer number if no parameter is used in the function. If two argument values are provided in this function, it will return a random integer number based on the argument values. The use of this function is shown below.

## Example 1: Different uses of rand () function

The following example shows the three different uses of the rand() function to generate a random number. At first, the rand() function is called two times without any argument to show how the random numbers are generated by default. Next, it is called two times with two different minimum and maximum values. At last, it is called two times with the bitwise operator.

<?php
//Use of rand() function without argument
echo "<h3>The random number using rand() without argument:</h3>";
echo "<p>The first random number: ". rand(). "</p>";
echo "<p>The second random number: ". rand(). "</p>";
//Use of rand() function with arguments
echo "<h3>The random number using rand() by defining the arguments:</h3>";
echo "<p>The first random number within the range [10-100]: ". rand(10,100). "</p>";
echo "<p>The second random number within the range [100-500]: ". rand(100,500). "</p>";
//Use of rand () function with bitwise operator
echo "<h3>The random number using rand () using bitwise operator:</h3>";
echo "The first random number using bitwise operator (&) with 10: ";
echo rand()&10 ;
echo "<br />The second random number using bitwise operator (&) with 50: ";
echo rand()&50 ;
?>

Output:

A similar output will appear after running the script from the server. The first output shows two different large numbers. In the second output, the first random number has been generated within the range of 10 to 100, and the second random number has been generated within the range of 100 to 500. In the third output, the first random number has been generated within the range of 0 to 10, and the second random number has been generated within the range of 0 to 50. ## Use of random_int()

This function is used to generate a cryptographically pseudo secure random number. The system call function getrandom(2) is used on Ubuntu to generate the cryptographic random number. This function is more secure than the rand() function because the generated number is not predictable. But random_int() is slower than rand() function. The syntax of this function is given below.

Syntax:

int random_int(int min, int max)

Two arguments are used in the function to set the range for generating a cryptographic random number. The first argument is used to set the lowest value, and the second argument is used to set the highest value of the number. The use of this function is shown below.

## Example 2: Different uses of random_int() function

The following example shows the use of the random_int() function to generate the random number by providing the minimum and maximum values. These values can be an integer or floating number, but the minimum cannot be larger than the maximum value. In the first random_int() function, the positive integer numbers are used as the minimum and maximum values. In the second random_int() function, the negative integer number is used as a minimum, and the positive integer number is used as a maximum value. In the third random_int() function, the floating numbers are used as the minimum and maximum values.

<?php

//Set the positive min and positive max values
echo "<p>The output of random_int() with the positive min and max values: <b>" .random_int(1000, 10000). "</b> </p>";
//Set the negative min and positive max values
echo "<p>The output of random_int() with the negative min and positive max values: <b>" .random_int(-500, 10000). "</b> </p>";
//Set the fractional min and max values
echo "<p>The output of random_int() with the fractional min and max values: <b>" .random_int(0.67, 54.89). "</b> </p>";
?>

Output:
The following similar output will appear after running the script from the server. ## Use of mt_rand ()

This function is used to generate high-quality pseudo-random numbers by using the Mersenne Twister generator. It works faster than the rand() function. The syntax of this function is given below.

Syntax:

int mt_rand(int min, int max)

Like the rand() function, it can take two arguments to set the max and min values, and this function can also be used without any arguments. The use of this function is shown below.

## Example 3: Different uses of mt_rand() function

The following example shows the use of mt_rand() function with and without the arguments. The first mt_rand() function is called without any argument that will generate a large integer number. The second mt_rand() function is called with the minimum and maximum value that will generate a number within the range defined.

<?php
//Use of mt_rand() function without argument
echo "<h3>The generated random number using mt_rand() without argument:</h3>";
echo "<p style='color:red'>". mt_rand(). "</p>";
//Use of mt_rand() function with the arguments
echo "<h3>The generated random number using mt_rand() by defining the arguments:</h3>";
echo "<p style='color:red'>". mt_rand(15,150). "</p>";
?>

Output:
The following similar output will generate after running the script from the server. The first output shows that a large integer number has been generated when no argument is used in the mt_rand() function. The second output shows that a number within the range of 15 to 150 has been generated. ## Conclusion

Three different ways to generate a random number in PHP have been explained in this tutorial using simple examples. It is better to use the random_int() function when security is important, but it is better to use the mt_rand() function when it is required to generate the random number faster. rand() function can be used for generating a simple random number. 