douqin231881 2012-07-17 22:58
浏览 325
已采纳

Rand函数产生不均匀的结果

In my program, the average value of $integers[0]->occurences should be 20000.Why it's always larger than 20000, even after using srand()?

<?php 
        define('NUM_OF_INTS',49);    
        define('DRAWS',1000000);

            class number
            {
               public $occurences;
               public $value;

               function number()                     
               {
                   $occurences=0;
                   $value=0;
               }
            }

            $integers = array();

            //srand(time(0));
            //initialising loop
            for($i=0;$i<=NUM_OF_INTS;$i++)
            {    
                $integers[$i] = new number(); 
                $integers[$i]->value = $i;

            }
            for($i=0;$i<DRAWS;$i++)
            {                  
                $integers[rand(0,NUM_OF_INTS)]->occurences++;               
            }

            foreach($integers as $int)
                printf("%5d %5d  <br/>",$int->value,$int->occurences);
        ?>
  • 写回答

2条回答

  • doutuo7126 2012-07-17 23:18
    关注

    You say that you're using WAMP.

    PHP's rand() isn't very random on Windows (it only generates 15 bits of randomness) so it's unable to distribute its random numbers evenly enough between the 50 bins.

    In particular, bin 0 will receive 656 / 32768 of the random numbers, which is about 0.0200195

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制