Home  >  Article  >  php教程  >  三个盒子,其中一个有奖,这时用户选择一个盒子,然后系统排除剩

三个盒子,其中一个有奖,这时用户选择一个盒子,然后系统排除剩

WBOY
WBOYOriginal
2016-06-06 19:37:213120browse

三个盒子,其中一个有奖,这时用户选择一个盒子,然后系统排除剩余两个盒子中没有奖的一个。问这时候在剩下的两个盒子中,重新选择和不重新选择的概率是多少 无 1 ?php 2 function win(){ 3 $arr = [0,0,0]; 4 $arr[rand(0,2)] =1; 5 //print_r($arr);; 6 $us

三个盒子,其中一个有奖,这时用户选择一个盒子,然后系统排除剩余两个盒子中没有奖的一个。问这时候在剩下的两个盒子中,重新选择和不重新选择的概率是多少
  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;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn