首页 >php教程 >PHP源码 >概率 PHP

概率 PHP

PHP中文网
PHP中文网原创
2016-05-26 08:19:271246浏览

概率

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)!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn