Copy codeThe Code is as follows:
<? Php
// Range is an array of columns from 1 to 100.
$ Numbers = range (1, 1,100 );
// Shuffle will immediately disrupt the array order
Shuffle ($ numbers );
// Array_slice retrieves a segment of the array
$ No = 6;
$ Result = array_slice ($ numbers, 0, $ no );
For ($ I = 0; $ I <$ no; $ I ++ ){
Echo $ result [$ I]. "<br> ";
}
Print_r ($ result );
?>
Copy codeThe Code is as follows:
// Range is an array of columns from 1 to 42.
$ Numbers = range (1, 42 );
// Shuffle will immediately disrupt the array order
Shuffle ($ numbers );
// Array_slice retrieves a segment of the array
$ Result = array_slice ($ numbers, 0, 3 );
Print_r ($ result );
Method 2
Copy codeThe Code is as follows:
<? Php
$ Numbers = range (1, 20 );
Srand (float) microtime () * 1000000 );
Shuffle ($ numbers );
While (list (, $ number) = each ($ numbers )){
Echo "$ number ";
}
?>
Method 3
With PHP, 5 non-repeated values are randomly generated between 1 and 20. How can this problem be solved?
Copy codeThe Code is as follows:
<? Php
Function NoRand ($ begin = 0, $ end = 20, $ limit = 5 ){
$ Rand_array = range ($ begin, $ end );
Shuffle ($ rand_array); // call the random array arrangement function.
Return array_slice ($ rand_array, 0, $ limit); // the first $ limit
}
Print_r (NoRand ());
?>
Or do not shuffle
Copy codeThe Code is as follows:
<? Php
$ Tmp = array ();
While (count ($ tmp) <5 ){
$ Tmp [] = mt_rand (1, 20 );
$ Tmp = array_unique ($ tmp );
}
Print join (',', $ tmp );
?>