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

后端 未结 8 865
忘掉有多难
忘掉有多难 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 04:53

    A call to Random.nextInt(n) will return an integer between 0 and n-1

    try temp = r.nextInt(250 - sum) + 1; and see if that solves your issue.

提交回复
热议问题