php - distribute a number without remainder -
i want distribute item specific pieces without cutting excess piece, example:
$persons = 7; $rooms = 2; for($i = 0; $i < $rooms; $i++) { //distribute persons per room } //the endpoint should $the_room[0] = 4 //instead of 3.5 $the_room[1] = 3 //instead of 3.5
how approach first equally distributes pieces, randomises remainder?
$persons = 7; $rooms = 2; $the_room = array(); for($i = 0; $i < $rooms; $i++) { $the_room[$i] = floor($persons / $rooms); } // random distribution while( $persons - array_sum($the_room) > 0) { $the_room[array_rand($the_room)]++; } // sequential distribution // $index = 0; // while( $persons - array_sum($the_room) > 0) // { // $the_room[$index++]++; // } print_r($the_room);
Comments
Post a Comment