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

后端 未结 4 1968
长情又很酷
长情又很酷 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:35

    I'm not sure whether I understood you correctly, but try this:

    $n = 4;
    $max = 30;
    $array = array();
    
    do {
        $random = mt_rand(0, $max);
    
        if (!in_array($random, $array)) {
            $array[] = $random;
            $n--;
        }
    } while (n > 0);
    

提交回复
热议问题