Home > Article > Backend Development > PHP implements a method to obtain N non-repeating random numbers within a certain range
This article mainly introduces the method of PHP to obtain N non-repeating random numbers within a certain range. It uses the range function to create an array within a specified range and shuffle to randomly sort the array, and uses array_slice to extract the array to achieve this function. It is very Simple and practical, friends in need can refer to
. The details are as follows:
//range 是将1000到9999 列成一个数组 $numbers = range (1000,9999); //shuffle 将数组顺序随即打乱 shuffle ($numbers); //array_slice 取该数组中的某一段 $result = array_slice($numbers,0,3); print_r($result);
The running result is:
Array ( [0] => 9767 [1] => 2344 [2] => 7783 )
Summary: The above is the entire content of this article, I hope it can help Everyone’s learning helps.
Related recommendations:
phpExample sharing of processing single file and multiple file uploads
PHP implementation between pages Methods of passing values and maintaining values
Methods to implement scheduled execution functions based on the sleep function in PHP
The above is the detailed content of PHP implements a method to obtain N non-repeating random numbers within a certain range. For more information, please follow other related articles on the PHP Chinese website!