2

I'm trying to make a little PHP script for fun, and I'm a little stuck.
I want to divide an integer (for example 5) over multiple values.
For example:

$total = 5;
$mike = 0;
$ralf = 0;
$ashley = 0;

// Run the magic here

echo "Mike has " . $mike . " apples, Ralf has " . $ralf ." apples and Ashley has " . $ashley . " apples";

The output that I expect would look something like this:
Mike has 2 apples, Ralf has 1 apples and Ashley has 2 apples

Is there a way how to do this? :)
I can't do this hard coded, because I want the values to be randomized.

Cheers

Finlay Roelofs
  • 533
  • 6
  • 21
  • 1
    something like this: http://stackoverflow.com/questions/7289136/how-to-make-5-random-numbers-with-sum-of-100 – ashanrupasinghe Mar 08 '17 at 10:33
  • Possible duplicate of [How to make 5 random numbers with sum of 100](http://stackoverflow.com/questions/7289136/how-to-make-5-random-numbers-with-sum-of-100) – LF00 Mar 08 '17 at 10:35
  • Do you want to divide the integer randomly, as everybody is assuming so far, or as equally as possible? – Just a student Mar 08 '17 at 10:38

3 Answers3

1

Do it like this:

$total = 5;
$mike = rand(1,$total-2); // so that max value is 3 (everyone should get at least 1) ($total - $numberOfVarsToDistributeTheValueTo + 1)
$ralf = rand(1,$total - $mike - 1); // if 3 goes to mike, only 1 goes to ralf
$ashley = $total - $mike - $ralf; // i hope you understand.


// use it.
mehulmpt
  • 15,861
  • 12
  • 48
  • 88
1

Something like this would work:

$people  = array('mike','ralf','ashley');
$num = count($people);
$sum  = 5; // TOTAL SUM TO DIVIDE
$groups = array();
$group = 0;     

    while(array_sum($groups) != $sum) {

        $groups[$group] = mt_rand(0, $sum/mt_rand(1,5));

        if(++$group == $num){
            $group = 0;
        }
    }

    // COMBINE ARRAY KEYS WITH VALUES
    $total = array_combine($people, $groups);

echo "Mike has " . $total['mike'] . " apples, Ralf has " . $total['ralf'] ." apples and Ashley has " . $total['ashley'] . " apples";    

Solution is inspired from this answer: https://stackoverflow.com/a/7289357/1363190

Community
  • 1
  • 1
Michael Krikorev
  • 2,126
  • 1
  • 18
  • 25
  • I preferred Mehul Mohan's answer, but since this is definitely helpful for when you want to add more people, I gave it a +1 :) – Finlay Roelofs Mar 08 '17 at 11:08
0

Hope This function will do your work . it can work for variable # of persons also.

divide_items(10,['mike','ralf','ashley']);
function divide_items($total=1,array $persons){
    $progressed = 0;
    for($i=0;$i<count($persons);$i++){
        echo $random_count = rand(1,$total); 
        if(($i==(count($persons)-1)) && $progressed<$total ){
            $random_count1 =$total - $progressed;
             echo $persons[$i]." has ".$random_count1 ." Items<br>";
            continue;
        }
        $progressed = $progressed+$random_count;

        if($progressed<=$total){
            echo $persons[$i]." has ".$random_count ." Items<br>";
        }else{
            echo $persons[$i]." has 0 Item<br>";
        }
        $total = $total-$random_count;
        $progressed = 0;

    }
}