Generate n random numbers whose sum is m and all numbers should be greater than zero

后端 未结 8 863
忘掉有多难
忘掉有多难 2020-12-18 04:04

I want to generate 9 non zero random numbers whose sum is 250. I have tried following code it gives me 9 random numbers but some numbers are zero.

 public vo         


        
8条回答
  •  鱼传尺愫
    2020-12-18 05:01

    I don't see where you've checked to make sure that zero is excluded. Add a check before you insert it into the array.

    Too many "magic numbers" in this code to suit me.

提交回复
热议问题