This article mainly and everyone introduced the PHP implementation of the color ball algorithm, involving the PHP array traversal, random number and other related operations skills, the need for friends can refer to, hope to help everyone.
<?phpfunction Doubleball () { $sysBlueball = Mt_rand (1,16); $sysRedball = Array (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33); $result = Array (); for ($i =0; $i <6; $i + +) { while (true) { $index = Mt_rand (0,32); if ($sysRedball [$index]! = 0) { $result [$i] = $sysRedball [$index]; $sysRedball [$index] = 0; Break ; }}} $result = Implode (', ', $result); Echo ' Your choice of basketball is: '. $sysBlueball. '; Echo ' You choose the red ball as: '. $result. ';} Doubleball ();? >
Operation Result:
The basketball you choose is: 3 The red ball you choose is: 29,11,26,14,9,24