This question has been asked before, but I've never really seen a good answer.
I want to generate 8 random numbers that sum to 0.5.
I want each number to be randomly chosen from a uniform distribution (ie, the simple function below will not work because the numbers will not be uniformly distributed).
def rand_constrained(n,tot): r = [random.random() for i in range(n)] s = sum(r) r = [(i/s*tot) for i in r] return r
The code should be generalizable, so that you can generate N uniform random numbers that sum to M (where M is a positive float). If possible, can you please also explain (or show with a plot) why your solution generates random numbers uniformly in the appropriate range?
Related questions that miss the mark:
Generate multiple random numbers to equal a value in python (current accepted answer isn't uniform--another answer which is uniform only works with integers)
Getting N random numbers that the sum is M (same question in Java, currently accepted answer is just plain wrong, also no answers with uniform distribution)
Generate N random integers that sum to M in R (same question, but in R with a normal--not uniform--distribution)
Any help is greatly appreciated.
Instead of selecting 'n' numbers from a uniform distribution that sum to 'M', we can select 'n-1' random interval from a uniform distribution that range '0-M' then we can extract the intervals.
Output
It is known as simplex sampling, which is closely related to Dirichlet distribution. Sum(x_i) = 1, where each x_i is U(0,1). In your case after simplex sampling just multiply each x_i by 0.5.
Anyway, translating c++ code from https://github.com/Iwan-Zotow/SimplexSampling into python (hopefully, not too many errors)
And it handles infinity just right
Same thing as k4vin's solution but without an import error which I'm getting on random.uniform.
But matplotlib is choking on installation so I can't plot it out right now.
For a fully generalized solution ("I want
n
numbers betweenlow
andhigh
, that sum tom
):For your case, this can be used as follows:
What you are asking for seems to be impossible.
However, I will re-interpret your question so that it makes more sense and is possible to solve. What you need is a probability distribution on the seven-dimensional hyperplane
x_1 + x_2 + ... + x_8 = 0.5
. Since the hyperplane is infinite in extent, a uniform distribution on the whole hyperplane will not work. What you probably(?) want is the chunk of the hyperplane where all thex_i>0
. That region is a simplex, a generalization of a triangle, and the uniform distribution on the simplex is a special case of the Dirichlet Distribution.You may find this section of the Dirichlet Distribution Wikipedia article, string cutting, particularly illuminating.
Implementation
The Wikipedia article gives the following implementation in Python in the Random Number Generation section:
What you probably(?) want is the case where all
ai=1
which results in a uniform distribution on the simplex. Herek
corresponds to the numberN
in your question. To get the samples to sum toM
instead of1
, just multiplysample
byM
.Update
Thanks to Severin Pappadeux for pointing out that gammavariate can return infinity under rare circumstances. That is mathematically "impossible" but can occur as an artifact of the implementation in terms of floating point numbers. My suggestion for handling that case is to check for it after
sample
is first calculated; if any of the components ofsample
are infinity, set all the non-infinity components to 0 and set all the infinity components to 1. Then when thexi
are calculated, outcomes likexi=1, all other x's=0
, orxi=1/2, xj=1/2, all other x's=0
will result, collectively "corner samples" and "edge samples".Another very-low-probability possibility is for the sum of the gammavariates to overflow. I would guess that we could run through the entire underlying pseudo-random number sequence and not see that happen, but theoretically it is possible (depending on the underlying pseudorandom number generator). The situation could be handled by rescaling
sample
, e.g., dividing all elements ofsample
byN
, after the gammavariates have been calculated but before the x's are calculated. Personally, I wouldn't bother because the odds are so low; program crashes due to other reasons would have higher probability.