Home >Backend Development >PHP Tutorial >PHP implements a method to obtain N non-repeating random numbers within a certain range

PHP implements a method to obtain N non-repeating random numbers within a certain range

墨辰丷
墨辰丷Original
2018-06-02 09:46:201900browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn