Home > Article > Backend Development > Use rand() function to generate a random number in PHP
During the use of PHP
, in some special circumstances, we may need to generate some random numbers for our use in certain scenarios. PHP
has a built-in rand()
function to help us solve this problem. This article will take you to take a look.
The first thing we need to understand is the syntax of the rand()
function:
rand ( int $min , int $max )
$min: Optional parameter, must be at the same time as $max Used to represent the minimum value of the generated number
$max: Optional parameter, which must be used together with $min, to represent the maximum value of the generated number
Return value: Returns an int value. If there are parameters, it returns $min to $max (including $min and $max). If there are no parameters, rand() returns 0 to getrandmax() Pseudo-random integers between
(getrandmax()
is the largest possible value for displaying random numbers)
Code example:
1. The function has no parameters
<?php echo rand();
输出:312803035//随机性,值不同
2. The function has parameters
<?php echo rand(10,100);
输出:15//随机性,值不同
Recommended: 《2021 PHP interview questions summary (collection)》《php video tutorial》
The above is the detailed content of Use rand() function to generate a random number in PHP. For more information, please follow other related articles on the PHP Chinese website!