Home > Article > Backend Development > Function to generate random numbers in php
The functions that generate random numbers in PHP are rand() and mt_rand(). rand() generates pseudo-random integers in the range 0 to PHP_INT_MAX, the sequence is generated based on the seed value. mt_rand() generates Mersenne rotated pseudo-random integers, providing better randomness, and requires explicitly setting the seed value to obtain a truly random sequence.
Function to generate random numbers in PHP
Answer the question:
The functions that generate random numbers in PHP are rand()
and mt_rand()
.
Detailed expansion:
##rand() Function:
produce a predictable sequence.
mt_rand() Function:
function.
Usage:
The random number function in PHP follows the following syntax:<code class="php">rand(min, max); // 获取 min 至 max 之间的随机整数(包括 min 和 max) mt_rand(min, max); // 获取 min 至 max 之间的梅森旋转随机整数(包括 min 和 max)</code>
Example :
<code class="php">// 生成 1 到 100 之间的随机整数 $randomNumber = rand(1, 100); // 生成梅森旋转随机数序列 mt_srand(); // 设置随机种子 for ($i = 0; $i < 10; $i++) { $randomNumber = mt_rand(); echo $randomNumber . "\n"; }</code>
The above is the detailed content of Function to generate random numbers in php. For more information, please follow other related articles on the PHP Chinese website!