Home > Article > Backend Development > How to generate N unique random numbers within a specified range in PHP?
PHP method to generate N non-repeating random numbers within the specified range: first store the values in the specified range into the array; then use [shuffle($array)] to disrupt the array; and finally Just intercept a certain number of values.
PHP method to generate N non-repeating random numbers within a specified range:
Idea: Generate By storing random numbers into an array, and then removing duplicate values from the array, a certain number of non-repeating random numbers can be generated.
In PHP website development, sometimes we need to generate a certain number of non-repeating random numbers within a specified range. How to specifically design this function to generate random numbers? We can store randomly generated numbers into an array, but by removing duplicate values while storing them, a certain number of non-repeating random numbers can be generated. Of course, you can also store the values within the specified range into an array, then use shuffle($array)
to disrupt the array, and then intercept a certain number of values. But the latter method will generate a larger array when the specified range of random numbers is too large.
Related learning recommendations: PHP programming from entry to proficiency
The code for the first approach is given below. The second approach is simpler. , you can try it, it’s almost the same
<?php /* * array unique_rand( int $min, int $max, int $num ) * 生成一定数量的不重复随机数,指定的范围内整数的数量必须 * 比要生成的随机数数量大 * $min 和 $max: 指定随机数的范围 * $num: 指定生成数量 */ function unique_rand($min, $max, $num) { $count = 0; $return = array(); while ($count < $num) { $return[] = mt_rand($min, $max); $return = array_flip(array_flip($return)); $count = count($return); } //打乱数组,重新赋予数组新的下标 shuffle($return); return $return; } //生成10个1到100范围内的不重复随机数 $arr = unique_rand(1, 100, 10); echo implode($arr, ","); ?>
The program runs as follows:
48,5,19,36,63,72,82,77,46,16
Additional remarks:
1. When generating random numbers, we used the mt_rand()
function. The average speed of this function to generate random numbers is several times faster than rand()
.
2. When removing duplicate values from the array, the "flip method" is used, which is to use array_flip()
to exchange the key and value of the array twice. This approach is much faster than using array_unique()
while removing duplicate values from the array.
3. Before returning the array, first use shuffle()
to assign a new key name to the array, ensuring that the key name is a consecutive number from 0-n. If you do not perform this step, it may cause the key names to be discontinuous when deleting duplicate values. If you use for to traverse, there will be problems. However, if you use foreach or do not need to traverse, you do not need shuffle.
Summary
The above is the entire content of this article. I hope the content of this article has certain reference and learning value for everyone’s study or work. Thank you for your review of the script. Home support. If you want to know more related content, please check the related links below
The above is the detailed content of How to generate N unique random numbers within a specified range in PHP?. For more information, please follow other related articles on the PHP Chinese website!