Getting N random numbers whose sum is M

RandomLanguage AgnosticSum

Random Problem Overview


I want to get N random numbers whose sum is a value.

For example, let's suppose I want 5 random numbers that sum to 1.

Then, a valid possibility is:

0.2 0.2 0.2 0.2 0.2

Another possibility is:

0.8 0.1 0.03 0.03 0.04

And so on. I need this for the creation of a matrix of belongings for Fuzzy C-means.

Random Solutions


Solution 1 - Random

Short Answer:

> Just generate N random numbers, compute their sum, divide each one by > the sum and multiply by M.

Longer Answer:

The above solution does not yield a uniform distribution which might be an issue depending on what these random numbers are used for. Another method proposed by Matti Virkkunen:

> Generate N-1 random numbers between 0 and 1, add the numbers 0 and 1 > themselves to the list, sort them, and take the differences of > adjacent numbers.

This yields a uniform distribution as is explained here

Solution 2 - Random

Generate N-1 random numbers between 0 and 1, add the numbers 0 and 1 themselves to the list, sort them, and take the differences of adjacent numbers.

Solution 3 - Random

I think it is worth noting that the currently accepted answer does not give a uniform distribution:

> "Just generate N random numbers, > compute their sum, divide each one by > the sum"

To see this let's look at the case N=2 and M=1. This is a trivial case, since we can generate a list [x,1-x], by choosing x uniformly in the range (0,1). The proposed solution generates a pair [x/(x+y), y/(x+y)] where x and y are uniform in (0,1). To analyze this we choose some z such that 0 < z < 0.5 and compute the probability that the first element is smaller than z. This probaility should be z if the distribution were uniform. However, we get

> Prob(x/(x+y) < z) = Prob(x < z(x+y)) = Prob(x(1-z) < zy) = Prob(x < y(z/(1-z))) = z/(2-2z).

I did some quick calculations and it appears that the only solution so far that appers to result in a uniform distribution was proposed by Matti Virkkunen:

> "Generate N-1 random numbers between 0 and 1, add the numbers 0 and 1 themselves to the list, sort them, and take the differences of adjacent numbers."

Solution 4 - Random

Unfortunately, a number of the answers here are incorrect if you'd like uniformly random numbers. The easiest (and fastest in many languages) solution that guarantees uniformly random numbers is just

# This is Python, but most languages support the Dirichlet.
import numpy as np
np.random.dirichlet(np.ones(n))*m

where n is the number of random numbers you want to generate and m is the sum of the resulting array. This approach produces positive values and is particularly useful for generating valid probabilities that sum to 1 (let m = 1).

Solution 5 - Random

In Java:

private static double[] randSum(int n, double m) {
	Random rand = new Random();
	double randNums[] = new double[n], sum = 0;
	
	for (int i = 0; i < randNums.length; i++) {
		randNums[i] = rand.nextDouble();
		sum += randNums[i];
	}

	for (int i = 0; i < randNums.length; i++) {
		randNums[i] /= sum * m;
	}

	return randNums;
}

Solution 6 - Random

To generate N positive numbers that sum to a positive number M at random, where each possible combination is equally likely:

  • Generate N exponentially-distributed random variates. One way to generate such a number can be written as—

      number = -ln(1.0 - RNDU())
    

    where ln(x) is the natural logarithm of x and RNDU() is a method that returns a uniform random variate 0 or greater and less than 1 (e.g., JavaScript's Math.random()). Note that generating the N numbers with a uniform distribution is not ideal because a biased distribution of random variate combinations will result. However, the implementation given above has several problems, such as being ill-conditioned at large values because of the distribution's right-sided tail, especially when the implementation involves floating-point arithmetic. Another implementation is given in another answer.

  • Divide the numbers generated this way by their sum.

  • Multiply each number by M.

The result is N numbers whose sum is approximately equal to M (I say "approximately" because of rounding error). See also the Wikipedia article Dirichlet distribution.

This problem is also equivalent to the problem of generating random variates uniformly from an N-dimensional unit simplex.


However, for better accuracy (compared to the alternative of using floating-point numbers, which often occurs in practice), you should consider generating n random integers that sum to an integer m * x, and treating those integers as the numerators to n rational numbers with denominator x (and will thus sum to m assuming m is an integer). You can choose x to be a large number such as 232 or 264 or some other number with the desired precision. If x is 1 and m is an integer, this solves the problem of generating random integers that sum to m.

The following pseudocode shows two methods for generating n uniform random integers with a given positive sum, in random order. (The algorithm for this was presented in Smith and Tromble, "Sampling Uniformly from the Unit Simplex", 2004.) In the pseudocode below—

  • the method PositiveIntegersWithSum returns n integers greater than 0 that sum to m, in random order,
  • the method IntegersWithSum returns n integers 0 or greater that sum to m, in random order, and
  • Sort(list) sorts the items in list in ascending order (note that sort algorithms are outside the scope of this answer).

 

METHOD PositiveIntegersWithSum(n, m)
    if n <= 0 or m <=0: return error
    ls = [0]
    ret = NewList()
    while size(ls) < n
      c = RNDINTEXCRANGE(1, m)
      found = false
      for j in 1...size(ls)
        if ls[j] == c
          found = true
          break
        end
      end
      if found == false: AddItem(ls, c)
    end
    Sort(ls)
    AddItem(ls, m)
    for i in 1...size(ls): AddItem(ret,
        ls[i] - ls[i - 1])
    return ret
END METHOD

METHOD IntegersWithSum(n, m)
  if n <= 0 or m <=0: return error
  ret = PositiveIntegersWithSum(n, m + n)
  for i in 0...size(ret): ret[i] = ret[i] - 1
  return ret
END METHOD

Here, RNDINTEXCRANGE(a, b) returns a uniform random integer in the interval [a, b).

Solution 7 - Random

  1. Generate N-1 random numbers.
  2. Compute the sum of said numbers.
  3. Add the difference between the computed sum and the desired sum to the set.

You now have N random numbers, and their sum is the desired sum.

Solution 8 - Random

> Just generate N random numbers, compute their sum, divide each one by > the sum.

Expanding on Guillaume's accepted answer, here's a Java function that does exactly that.

public static double[] getRandDistArray(int n, double m)
{
    double randArray[] = new double[n];
    double sum = 0;
    
    // Generate n random numbers
    for (int i = 0; i < randArray.length; i++)
    {
        randArray[i] = Math.random();
        sum += randArray[i];
    }
    
    // Normalize sum to m
    for (int i = 0; i < randArray.length; i++)
    {
        randArray[i] /= sum;
        randArray[i] *= m;
    }
    return randArray;
}

In a test run, getRandDistArray(5, 1.0) returned the following:

[0.38106150346121903, 0.18099632814238079, 0.17275044310377025, 0.01732932296660358, 0.24786240232602647]

Solution 9 - Random

You're a little slim on constraints. Lots and lots of procedures will work.

For example, are numbers normally distributed? Uniform?
I'l assume that all the numbers must be positive and uniformly distributed around the mean, M/N.

Try this.

  1. mean= M/N.
  2. Generate N-1 values between 0 and 2mean. This can be a standard number between 0 and 1, u, and the random value is (2u-1)*mean to create a value in an appropriate range.
  3. Compute the sum of the N-1 values.
  4. The remaining value is N-sum.
  5. If the remaining value does not fit the constraints (0 to 2*mean) repeat the procedure.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmarionmaidenView Question on Stackoverflow
Solution 1 - RandomGuillaumeView Answer on Stackoverflow
Solution 2 - RandomMatti VirkkunenView Answer on Stackoverflow
Solution 3 - RandomAccipitridaeView Answer on Stackoverflow
Solution 4 - RandomcgnorthcuttView Answer on Stackoverflow
Solution 5 - RandomVorticoView Answer on Stackoverflow
Solution 6 - RandomPeter O.View Answer on Stackoverflow
Solution 7 - RandomYuvalView Answer on Stackoverflow
Solution 8 - RandomStevoisiakView Answer on Stackoverflow
Solution 9 - RandomS.LottView Answer on Stackoverflow