For 36 numbers between 1 and 36, 5 are randomly selected to make the total number 100.

Source: Internet
Author: User
For 36 numbers between 1 and 36, 5 are randomly selected to make the total number 100.

I was a friend who asked me to write a method in excel: In the case of 36 numbers between 1 and 36, 5 were randomly selected to make the sum of 100:

I am not very familiar with excel, so I try to write a method using php:

Imagine:If the five numbers fluctuate around 20, and the fluctuation values offset each other, then the random number can be easily obtained.

Ideas:A_random + B _random + a_offset + c_random + B _offset + d_random + c_offset + e_random

Where a_offset is the fluctuation value of a_random and 20 in the middle, it indicates a_random + a_offset = 20, similarly, B _random + B _offset = 20, etc.

Then (a_random + a_offset) + (B _random + B _offset) + (c_random + c_offset) + d_random + e_random = 60 + d_random + e_random;

The last number e_random is not random. it is obtained from 100-60-d_random. you only need to randomly output d_random and then judge whether it meets the requirements.
If you have a better method, please follow me and tell me, thank you!

  1. // Parameter settings
  2. $ Total = 100;
  3. $ Min_num = 1;
  4. $ Max_num = 36;
  5. $ Times = 5;
  6. $ Average = $ total/$ times;
  7. $ Now = 0;
  8. For ($ I = 0; $ I <$ times; $ I ++)
  9. {
  10. $ Off = 0;
  11. $ Tmp = 0;
  12. If ($ I = $ times-1)
  13. {
  14. $ Tmp = $ total-$ now;
  15. If ($ tmp> $ max_num | $ tmp <0)
  16. {
  17. $ Off = $ now = 0;
  18. $ I = 0;
  19. // Echo "$ tmp |"; // data that does not meet the requirements
  20. Unset ($ num );
  21. $ Tmp = rand ($ min_num, $ max_num );
  22. $ Off = $ tmp-$ average;
  23. $ Now = $ now + $ tmp;
  24. $ Num [] = $ tmp;
  25. Continue;
  26. }
  27. Else
  28. {
  29. $ Num [] = $ tmp;
  30. Break;
  31. }
  32. }
  33. If ($ off = 0)
  34. {
  35. $ Tmp = rand ($ min_num, $ max_num );
  36. }
  37. Else
  38. {
  39. $ Tmp = rand ($ min_num-$ off, $ max_num-$ off );
  40. $ Tmp = ($ tmp + $ off );
  41. }
  42. $ Off = $ tmp-$ average;
  43. $ Now = $ now + $ tmp;
  44. $ Num [] = $ tmp;
  45. }
  46. // Test
  47. $ Con = '';
  48. Foreach ($ num as $ val)
  49. {
  50. $ Total_my = $ total_my + $ val;
  51. Echo $ con. "$ val ";
  52. $ Con = "+ ";
  53. }
  54. Echo '='. $ total_my;
  55. Exit;
  56. ?>

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.