mercredi 31 mai 2017

c++ vector of unique random positive numbers with fixed sum

I thought that it would be easier at the beginning...

I have an std::vector with fixed size (ie 5 elements). I want every element of the vector to be randomly selected between two positive numbers (zeros are not permitted) but the random numbers should sum to a constant value.

For example

vector size = 5;
sum = 20;
element min = 1;
element max = 10;

accepted answer should be [2, 9, 5, 4, 1]

Any ideas on how to do this fast?




Aucun commentaire:

Enregistrer un commentaire