Random percentage allocation based on number of people

Source: Internet
Author: User
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 );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.