Random numbers that add to 100: Matlab

前端 未结 4 1821
死守一世寂寞
死守一世寂寞 2020-11-22 07:25

[I\'m splitting a population number into different matrices and want to test my code using random numbers for now.]

Quick question guys and thanks for your help in a

4条回答
  •  醉梦人生
    2020-11-22 08:24

    One simple way is to pick 8 random numbers between 0 and 100. Add 0 and 100 to the list to give 10 numbers. Sort them. Then output the difference between each successive pair of numbers. For example, here's 8 random numbers between 0 and 100:

    96, 38, 95, 5, 13, 57, 13, 20

    So add 0 and 100 and sort.

    0, 5, 13, 13, 20, 38, 57, 95, 96, 100

    Now subtract:

    5-0 = 5
    13-5 = 8
    13-13 = 0
    20-13 = 7
    38-20 = 18
    57-38 = 19
    95-57 = 38
    96-95 = 1
    100-96 = 4

    And there you have it, nine numbers that sum to 100: 0, 1, 4, 5, 7, 8, 18, 19, 38. That I got a zero and a one was just a strange bit of luck.

提交回复
热议问题