To generate random data in php, we can use rand, mt_rand can generate random data within a specified range. Let’s introduce the method to students
Call mt_rand()This method Random numbers can be generated, the parameters are the minimum and maximum values of the range, and the function returns a random number between the minimum and maximum values.
To generate truly random numbers is not an easy task for calculation.
There are two methods in PHP to generate random numbers, one is a classic function called rand(), and the other is a more outstanding function is mt_rand().
Example 1
The code is as follows
$random =rand(0,1000);
or
<?php $rand = mt_rand(1, 100); echo $rand; ?>
Example 2
The code is as follows
srand((double)microtime()*1000000); $random =rand(0,1000);
Example 3
The code is as follows
/** *获取一定范围内的多个随机数字 */ function yang_numberRand($begin = 0, $end = 20, $limit = 5){ $rand_array = range($begin, $end); shuffle($rand_array); //调用现成的数组随机排列函数 return array_slice($rand_array, 0, $limit); //截取前$limit个 }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
Detailed explanation of PHP thumbnail generation and image watermark production
PHP verification code class ValidateCode analysis
phpUse the preg_match() function to implement the method of verifying the ip address
The above is the detailed content of PHP method to implement multiple random numbers within a specified range. For more information, please follow other related articles on the PHP Chinese website!