如何使用php获得不同时间间隔内的分组随机数

如何使用php获得不同时间间隔内的分组随机数,php,Php,我没有发现我的代码有任何问题,但它只输出从0到20的数字,没有遵守我强加的时间限制。请帮帮我。代码如下: <!DOCTYPE html> <html> <?php set_time_limit(0); class random{ var $max; var $min; function irregularnos($min,$max) { $num = '';

我没有发现我的代码有任何问题,但它只输出从0到20的数字,没有遵守我强加的时间限制。请帮帮我。代码如下:

<!DOCTYPE html>
<html>
<?php
    set_time_limit(0);
    class random{
        var $max;
        var $min;
        function irregularnos($min,$max) {
             $num = '';
             $i = 0;
             $this->min=$min;
             $this->max=$max;
             while($i < 25)
             {
                 $num .= mt_rand($min,$max);
                 echo $num . "</br>";
                 $num = '';
                 $i++;
             }
         }
    } 
    $rand1 = new random;
    $rand2 = new random;
    $rand3 = new random; 
    $rand4 = new random;
    date_default_timezone_set('Europe/Berlin');
    $start_time = new DateTime(); //current time 
    while(true){
        if ($start_time->modify('+1 seconds')<= new DateTime() and  $start_time->modify('+2      seconds') > new DateTime()) { $rand2->irregularnos(21,50);}
else  if ($start_time->modify('+2 seconds') <= new DateTime() and $start_time-   >modify('+3 seconds') > new DateTime()) { $rand3->irregularnos(51,70);}
else if ($start_time->modify('+3 seconds') <= new DateTime() and $start_time->modify('+4 seconds') > new DateTime()) {$rand4->irregularnos(71,100);}
else if (new DateTime() < $start_time->modify('+1 seconds')) {$rand1->irregularnos(0,20);}
else if(new DateTime()> $start_time->modify('+4 seconds'))  { break;}
    } 
?>

代码正在运行。可能您希望代码在每个随机数中只运行一次,而实际上循环可能会执行很多次,比如说,在第一秒钟内执行50次

对于整秒钟,接下来的条件是时间介于
start\u time
start\u time+1秒之间,因此0-20序列被激活

另外,请注意,您没有正确使用对象-例如,实例变量min和max从未实际使用过,并且您的调用原型将使声明四个数字生成器变得毫无意义

此代码增加了一个暂停,以显示生成的所有数字:

<?php
    set_time_limit(0);
    class random{
        var $min;
        var $max;
        function __construct($min, $max) {
                $this->min = $min;
                $this->max = $max;
        }
        function irregularnos($n) {
            for ($i = 0; $i < $n; $i++) {
                $num = mt_rand($this->min, $this->max);
                echo $num . '</br>';
             }
         }
    }
    $rand1 = new random(0,20);
    $rand2 = new random(21,50);
    $rand3 = new random(51,70);
    $rand4 = new random(71,100);
    date_default_timezone_set('Europe/Berlin');
    $start_time = time();
    for($quit = false; !$quit;) {
        // How much time has elapsed since $start_time?
        switch(time() - $start_time)
        {
                case 0: $rand1->irregularnos(25); break;
                case 1: $rand2->irregularnos(25); break;
                case 2: $rand3->irregularnos(25); break;
                case 3: $rand4->irregularnos(25); break;
                default: $quit = true; break;
        }
        print "---\n";
        usleep(500000);
    }
?>


它通常以缩进代码开始。您需要描述您正在尝试做什么,以及到底有什么问题需要人们帮助。甚至可以自己用这种方式解决问题!