Create numbers within an array that add up to a set amount

后端 未结 4 1967
长情又很酷
长情又很酷 2021-02-07 10:03

I\'m fairly new to PHP - programming in general. So basically what I need to accomplish is, create an array of x amount of numbers (created randomly) whose value add up

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-07 10:42

    Hope this will help you more....

    Approch-1

    $aRandomarray = array();
    for($i=0;$i<100;$i++)
    {
        $iRandomValue = mt_rand(1000, 999);
        if (!in_array($iRandomValue , $aRandomarray)) {
            $aRandomarray[$i] = $iRandomValue;
        }
    } 
    

    Approch-2

    $aRandomarray = array();
    for($i=0;$i<100;$i++)
    {
        $iRandomValue = mt_rand(100, 999);
        $sRandom .= $iRandomValue;
    }
    array_push($aRandomarray, $sRandom);
    

提交回复
热议问题