dimanche 10 décembre 2017

How to generate a random sequence of numbers between 2 numbers with a fixed sum in C#?

So I am trying to generate a sequence of N numbers where the sum will be S.

Right now I am doing this:

float baseamount = (float)(amount / norders) * 0.8f;
float secondamount = amount / norders;

then generate N random numbers between these 2 numbers.

However this generates a pretty uniform range of numbers whereas I want a bit more randomized, for example, instead of (sum = 200, n = 10):

16.92321 17.49378 16.26426 16.03404 16.12497 17.53131 18.10094 16.86982 17.0831 16.06921

I prefer:

12.345 17.4534 19.3542342 11.345345 18.4325235 14.4353245 ...

What's the best way to approach this?




Aucun commentaire:

Enregistrer un commentaire