Home  >  Article  >  Backend Development  >  PHP n unique random number generation code example

PHP n unique random number generation code example

怪我咯
怪我咯Original
2017-07-11 11:40:221166browse

Whether it is a web application, a WAP or a mobile application, random numbers have their place. This article introduces PHP n non-repeating random number generation code examples

The code is as follows:

<?php 
//range 是将1到100 列成一个数组 
$numbers = range (1,100); 
//shuffle 将数组顺序随即打乱 
shuffle ($numbers); 
//array_slice 取该数组中的某一段 
$no=6; 
$result = array_slice($numbers,0,$no); 
for ($i=0;$i<$no;$i++){ 
echo $result[$i]."<br>"; 
} 
print_r($result); 
?>

The code is as follows:

//range 是将1到42 列成一个数组 
$numbers = range (1,42); 
//shuffle 将数组顺序随即打乱 
shuffle ($numbers); 
//array_slice 取该数组中的某一段 
$result = array_slice($numbers,0,3); 
print_r($result);

The above is the detailed content of PHP n unique random number generation code example. 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