1 <?php 2 function win(){ 3 $arr = [0,0,0]; 4 $arr[rand(0,2)] =1; 5 //print_r($arr);; 6 $userF=rand(0,2); 7 $userFirst = $arr[$userF]; 8 while(1){ 9 $delete = rand(0,2); 10 if($arr[$delete] === 0 && $delete !== $userF){ 11 unset($arr[$delete]); 12 break; 13 } 14 } 15 while(1){ 16 $select = rand(0,2); 17 if($select !== $delete && $select !== $userF){ 18 break; 19 } 20 } 21 $userSecond = $arr[$select]; 22 return array($userFirst,$userSecond); 23 } 24 $userFirstNum =0; 25 $userScondNum =0; 26 for($i=1;$i<=100000;$i++){ 27 $arr = win(); 28 ($arr[0]===1) && $userFirstNum++; 29 ($arr[1]===1) && $userScondNum++; 30 } 31 echo $userFirstNum.PHP_EOL; 32 echo $userScondNum;
以上就是概率的内容,更多相关内容请关注PHP中文网(www.php.cn)!