随机概率问题

8:设随机抽到A的概率为0.1,B的概率为0.2,C的概率为0.3,D的概率为0.4,现在求按此概率随机抽出一个字母的算法

 

 
 
  1. <?php  
  2. /*  
  3. *8:设随机抽到A的概率为0.1,B的概率为0.2,C的概率为0.3,D的概率为0.4,现在求按此概率随机抽出一个字母的算法,  
  4. *系统环境:windows/linux  
  5. *编译环境:php4/php5  
  6. *输入参数:存放在in.txt,多个参数时空格分隔  
  7.                     参数1是一个数字,表示测试循环次数  
  8.  输出:out.txt  
  9. */ 
  10.  
  11. //输出结果  
  12. //output(getRandomLetter(),true);  
  13. $params=getParams(1);  
  14. $argv0=trim($params[0]);  
  15. if(!is_numeric($argv0))  
  16. {  
  17.     error_msg("params 1 must be a numbers");  
  18. }  
  19. //测试几率  
  20. $test=array();  
  21. for($i=0;$i$argv0;$i++)  
  22. {  
  23.     $letter=getRandomLetter();  
  24.     if(!isset($test[$letter]))  
  25.     {  
  26.         $test[$letter]=0;  
  27.     }  
  28.     $test[$letter]++;  
  29. }  
  30. output("",true);  
  31. foreach($test as $k => $v)  
  32. {  
  33.     output("get $k times:$v");  
  34. }  
  35.  
  36.  
  37. error_msg("execute success");  
  38.  
  39. function getRandomLetter()  
  40. {  
  41.     //随机数基数  
  42.     $MaxRand=10000;  
  43.     $percentA=0.1*$MaxRand//获得A的几率1~$percentA;  1-1000  
  44.     $percentB=$percentA+0.2*$MaxRand//获得A的几率$percentA+1~$percentB   1000~3000  
  45.     $percentC=$percentB+0.3*$MaxRand//获得A的几率$percentB+1~$percentC  3000~6000  
  46.     $percentD=$percentC+0.4*$MaxRand//获得A的几率$percentC+1~$percentD  6000~10000  
  47.     $r=rand(1, $MaxRand);  
  48.     if($r >= 1 && $r <= $percentA)  
  49.     {  
  50.         return "A";  
  51.     }  
  52.     if($r >= $percentA+1 && $r <= $percentB)  
  53.     {  
  54.         return "B";  
  55.     }  
  56.       
  57.     if($r >= $percentB+1 && $r <= $percentC)  
  58.     {  
  59.         return "C";  
  60.     }  
  61.     if($r >= $percentC+1 && $r <= $percentD)  
  62.     {  
  63.         return "D";  
  64.     }  
  65.     return "";  
  66. }  
  67.  
  68.  
  69. /*  
  70.     从in.txt里读取参数  
  71. */ 
  72. function getParams($paramNum)  
  73. {  
  74.     $in=file_get_contents("in.txt");  
  75.     if($in===FALSE){  
  76.         error_msg("cannot read in.txt,please check in.txt exists\n");     
  77.     }  
  78.     $in=preg_replace("/(\s+)/i"" "$in);  
  79.     //多个参数时,按照空格分隔  
  80.     $parms=split(" ",trim($in));  
  81.     if($parms===FALSE)  
  82.     {  
  83.         error_msg("cannot get param from in.txt\n");  
  84.     }  
  85.     if(count($parms) < $paramNum)  
  86.     {  
  87.         error_msg("it needs $paramNum params\n");  
  88.     }  
  89.     return $parms;  
  90. }  
  91.  
  92. /*  
  93.     把结果输出到输出文件里  
  94.     当isClean=true时清空out.txt  
  95. */ 
  96. function output($msg,$isClean=false)  
  97. {  
  98.     if($isClean)  
  99.     {  
  100.     $handle = fopen('out.txt''w');  
  101.     fclose($handle);      
  102.     }  
  103.     error_log($msg."\n", 3, "out.txt");  
  104. }  
  105. /*  
  106.     输入错误信息  
  107.     如果$is_exit表示输入信息后退出  
  108. */ 
  109. function error_msg($msg,$is_exit=true)  
  110. {  
  111.     if($is_exit)  
  112.         die($msg."\n");  
  113.     else 
  114.         echo $msg."\n";  
  115. }  
  116. ?> 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值