mercredi 13 janvier 2016

Select K unique random numbers from range with sum equal to S

i have a range

R = {0, ..., N}

and i like to get K elements which have a sum equal to S, but the elements should be selected randomly.

So an easy brute force method would be to determine all element combinations containing K numbers resulting in S and picking one of the combinations by random.

I am trying to think about a recursive solution where a random number is selected and then the problem reduces to find (K-1) random numbers with sum equal to (S - K0) but this need not yield in a solution.

Is there a better approach?

A sample would be:

R = {0,1,2,3,4,5}, S = 5, K = 2
Solutions: randomly pick one of {{1,4};{2,3}}




Aucun commentaire:

Enregistrer un commentaire