The requirement for random percentage allocation is as follows. the variable parameter $ people is used to randomly assign a percentage to each person, provided that the number of people is limited to 3 <N <100, the weak condition is to ensure that the proportion allocated to each person is at least 30% and no extreme value exists.
Write a simple assignment, but the last person will get a lot of results, and because the allocation interval is actually the percentage between 1-average, it's not a strictly "random mean allocation". I 'd like to thank you for giving me a great idea.
$ People = 3; // number of people $ percent = 100; // percentage $ average = floor ($ percent/$ people); $ rand_array = array (); $ count = 0; for ($ I = 0; $ I <$ n; $ I ++) {if ($ I = ($ n-1 )) {$ rand_array [$ I] = 100-$ count;} else {$ rand_array [$ I] = rand (1, $ average ); $ count = $ count + $ rand_array [$ I] ;}return $ rand_array;
Reply to discussion (solution)
This may be better.
$ People = 3; // number of people $ percent = 100; // percentage $ res = array_fill (0, 3, floor ($ percent/$ people )); // average Distribution $ d = 5; // tolerance foreach ($ res as & $ v) $ v + = rand (-$ d, $ d ); $ res [rand (0, $ people-1)] + = $ percent-array_sum ($ res); // randomly add the residual value to print_r ($ res );
I forgot to close the post. thanks to the moderator for his great reply. The problem has been solved and I will share it with you:
Public static function rand_bouns ($ person) {// percentage $ percent = 100; $ now_person = $ person; $ bouns = array (); for ($ I = 0; $ I <= $ person-1; $ I ++) {$ bouns [$ I] = self: get_bouns ($ now_person, $ percent ); $ percent = $ percent-$ bouns [$ I]; $ now_person = $ now_person-1; $ now_bouns + = $ bouns [$ I];} return $ bouns ;} public static function get_bouns ($ person, $ percent) {if ($ person = 1) return $ percent; $ max = 30; if ($ percent <$ max) $ ma X = $ percent; $ min = $ percent-$ max * ($ person-1) <= 0? 1: $ percent-$ max * ($ person-1); $ max = $ max-($ person) <= 0? 1: $ max-($ person); return rand ($ min, $ max );}