Getting N random numbers that the sum is M

2019-01-01 07:37发布

I want to get N random numbers that the sum of them is a value.

For example, let's suppose I want 5 random numbers that their sum is 1

Then, a valid possibility is:

0.2 0.2 0.2 0.2 0.2

Other possibility is:

0.8 0.1 0.03 0.03 0.04

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

9条回答
永恒的永恒
2楼-- · 2019-01-01 07:56

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 an 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.

I'm not sure if this yield an uniform distribution

查看更多
看淡一切
3楼-- · 2019-01-01 08:00

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]
查看更多
还给你的自由
4楼-- · 2019-01-01 08:02
  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.

查看更多
余欢
5楼-- · 2019-01-01 08:03

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 2*mean. This can be a standard number between 0 and 1, u, and the random value is (2*u-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.
查看更多
君临天下
6楼-- · 2019-01-01 08:04

This problem is equivalent to the problem of generating random numbers with a Dirichlet distribution. To generate N positive numbers that sum to a positive number M, where each possible combination is equally likely:

  • Generate N exponentially-distributed random numbers. 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 random number 0 or greater and less than 1 (e.g., JavaScript's Math.random()). Note that generating those numbers with a uniform distribution is not ideal because a biased distribution of random number combinations will result.

  • Divide the numbers generated this way by their sum.
  • Multiply each number by M.

The result is N numbers in a Dirichlet distribution whose sum is approximately equal to M (I say "approximately" because of rounding error).

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

查看更多
初与友歌
7楼-- · 2019-01-01 08:06

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.

查看更多
登录 后发表回答