Php: a simple method for obtaining random combinations from a specified number (recommended). php numbers
For example:Given the number 100, three combinations of the numbers must be randomly obtained, for example, 10.
The Code is as follows:
<? Php/*** obtain the random number combination of a specified number * @ param Int $ var Number * @ param Int $ Number of the combination of num * @ return Array */function getNumGroups ($ var, $ num) {// incorrect quantity if ($ var <$ num) {return array () ;}$ total = 0; $ result = array (); for ($ I = 1; $ I <$ num; $ I ++) {$ tmp = mt_rand (1, $ var-($ num-$ I) -$ total); $ total + = $ tmp; $ result [] = $ tmp;} $ result [] = $ var-$ total; return $ result ;} // demo $ result = getNumGroups (100, 3); print_r ($ result);?>
Output:
Array ([0] => 42 [1] => 25 [2] => 33)
The simple method (recommended) for getting a random combination from a specified number in php is all the content shared by the editor. I hope you can give us a reference and support for the house of friends.