Code for generating non-repeated PHPn random numbers. Copy the code as follows :? Phprange is an array consisting of 1 to 100 columns $ numbersrange (1,100); shuffle disconnects the array order immediately shuffle ($ numbers); array_slice obtains the array
The code is as follows:
// 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]."
";
}
Print_r ($ result );
?>
The 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
The code is as follows:
$ 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?
The code is as follows:
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
The code is as follows:
$ Tmp = array ();
While (count ($ tmp) <5 ){
$ Tmp [] = mt_rand (1, 20 );
$ Tmp = array_unique ($ tmp );
}
Print join (',', $ tmp );
?>
The http://www.bkjia.com/PHPjc/320353.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/320353.htmlTechArticle code is as follows :? Php // range is an array of columns from 1 to 100. $ numbers = range (1,100); // shuffle disconnects the array order and shuffle ($ numbers ); // array_slice fetch the array...