We know that the sum of n numbers is 32898. calculate the number of each addition and the perfect version.

Source: Internet
Author: User
We know that the sum of n numbers is 32898. calculate the sum of each number and find that the original post of the perfect version has been closed. The result is not perfect. The maximum number of 999 duplicates is always too large. it seems random and a bit false.
Http://bbs.csdn.net/topics/391814341

$ R = foo (32898, 53); echo array_sum ($ r), PHP_EOL; // verify the sum of print_r (array_count_values ($ r )); // view the distribution function foo ($ num, $ k, $ min = 1, $ max = 999) {$ res = array_fill (0, $ k, 1 ); do {for ($ I = 0; $ I <$ k; $ I ++) {$ sum = array_sum ($ res); $ t = rand (0, $ max-$ min); if ($ res [$ I] + $ t> $ max) $ t = $ max-$ res [$ I]; if ($ sum + $ t> $ num) $ t = $ num-$ sum; $ res [$ I] + = $ t ;}} while ($ num> $ sum); return $ res ;}


Please fix this problem. thank you.


Reply to discussion (solution)

$ T = rand (0, $ max-$ min );
Change
$ T = rand (0, $ max-$ res [$ I]);

This distribution should be good.

Array(    [886] => 1    [760] => 1    [470] => 1    [875] => 1    [591] => 1    [499] => 1    [257] => 1    [954] => 1    [264] => 1    [615] => 1    [770] => 1    [644] => 1    [971] => 1    [979] => 1    [259] => 1    [906] => 1    [830] => 1    [537] => 1    [595] => 1    [531] => 1    [588] => 1    [748] => 1    [767] => 1    [844] => 1    [931] => 1    [835] => 1    [986] => 1    [976] => 1    [89] => 1    [157] => 1    [408] => 1    [582] => 1    [593] => 1    [876] => 1    [710] => 1    [130] => 1    [554] => 1    [774] => 1    [386] => 1    [339] => 1    [686] => 1    [194] => 1    [903] => 1    [304] => 1    [207] => 1    [978] => 1    [232] => 1    [618] => 1    [834] => 1    [472] => 1    [839] => 1    [377] => 1    [788] => 1)

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.