Get a randomized aberrancy around given value

后端 未结 4 2061
栀梦
栀梦 2021-01-27 02:30

I would like to add a kind of ripple to an array of known values of type double. I point that out because Random.Next / Random.NextDouble() behave different.

4条回答
  •  余生分开走
    2021-01-27 02:58

    One way you could do this is to generate N random numbers between 0 and 1 (exclusive). Sum them. Then divide each number by the sum. You now have a list of N random numbers that sum to 1. Now, multiply each of those numbers by your desired sum to get the numbers that will go into your final array.

    If you want your values to be +/- some percentage, then use Random.Next to generate the random numbers within some range and sum them. Then divide by the total to get the list of numbers that sums to 1. The final step is the same.

提交回复
热议问题