Home > Backend Development > PHP Tutorial > vb random number PHP n non-repeating random number generation code

vb random number PHP n non-repeating random number generation code

WBOY
Release: 2016-07-29 08:40:09
Original
1627 people have browsed it

Copy the code The code is as follows:


//range is to list 1 to 100 into an array
$numbers = range (1,100);
//shuffle will disrupt the order of the array
shuffle ($numbers);
//array_slice Get a certain segment in the array
$no=6;
$result = array_slice($numbers,0,$no);
for ($i=0;$i<$ no;$i++){
echo $result[$i]."
";
}
print_r($result);
?>


Copy code The code is as follows:


//range is to list 1 to 42 into an array
$numbers = range (1,42);
//shuffle will disrupt the order of the array
shuffle ($numbers);
//array_slice takes the array A certain section
$result = array_slice($numbers,0,3);
print_r($result);


Method 2

Copy code The code is as follows:


$numbers = range (1,20);
srand ((float)microtime()*1000000);
shuffle ($numbers);
while (list (, $number) = each ($numbers)) {
echo "$number " ;
}
?>

Method 3
Use PHP to randomly generate 5 unique values ​​between 1-20. How to do it

Copy the code The code is as follows:


function NoRand($begin=0,$end=20,$limit=5){
$rand_array=range($begin,$end);
shuffle($rand_array);//Call the ready-made array random arrangement function
return array_slice($rand_array,0,$limit);//Intercept the first $limit
}
print_r(NoRand());
?>


or if not shuffle

Copy the code The code is as follows:


$tmp=array();
while(count($tmp)<5){
$tmp[]=mt_rand(1,20);
$tmp=array_unique($tmp) ;
}
print join(',',$tmp);
?>

The above introduces the vb random number PHP n non-repeating random number generation code, including the vb random number content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template