开发者

about random values [duplicate]

开发者 https://www.devze.com 2023-01-29 15:35 出处:网络
This question already has answers here开发者_开发技巧: Closed 12 years ago. Possible Duplicate: Getting N random numbers that the sum is M
This question already has answers here开发者_开发技巧: Closed 12 years ago.

Possible Duplicate:

Getting N random numbers that the sum is M

Hi I have a question that:

how can i get random values where the sum of all of them is 1. like {0.5,0.5} or {0.25,0.25,0.5} and more. also the number of these values are different every time ,once can be 2 and once can be 3 like the example above! thanks.


I'll outline the basic algorithm for you:

  • decide how many random numbers you will generate that will be summed.

  • generate that many random numbers.

  • decide what they should all add up to.

  • divide number from the previous step by the sum of the random numbers.

  • divide each random number by the number from the previous step.

Basically what you'll be doing is generating a bunch of unbounded random numbers and then adjusting them all so they all add up to some specific number.

BTW: In the very unlikely event that all the random numbers you generate are zero, you'll have a divide by zero error with this algorithm. So you should trap for that in your implementation and retry the random number generation in a loop until you get a non-zero sum of random numbers.


In general you can generate an array of random size:

    java.util.Random rand = new java.util.Random();
    final int MAX_SIZE = 100;
    double[] a = new double[1 + rand.nextInt(MAX_SIZE)];

(Here I'm assuming you will not want arrays larger than 100 elements.)

Then you fill the array with random positive numbers:

    for (int i = 0; i < a.length; ++ i)
    {
        a[i] = rand.nextDouble();
    }

Then you normalize the array (divide each element by the total sum). First we compute the total sum:

    double sum = 0.0;
    for (int i = 0; i < a.length; ++ i)
    {
        sum += a[i];
    }

Then divide each array element by the sum:

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

If you want shorter code, you can combine the loop that accumulates the sum with the loop that fills the array with random positive integers. Here is the resulting code:

    java.util.Random rand = new java.util.Random();
    final int MAX_SIZE = 100;
    double[] a = new double[1 + rand.nextInt(MAX_SIZE)];

    double sum = 0.0;
    for (int i = 0; i < a.length; ++ i)
    {
        a[i] = rand.nextDouble();
        sum += a[i];
    }

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


Well, I think that the easiest way would be something like this:

To generate two numbers:

r1 = new RandomNumber
r2 = 1 - r1

TO generate three numbers:

r1 = new RandomNumber
r2 = r1 / 2
r3 = 1 - r1
r1 = r1 / 2

I do not know if there is any simpler way of doing this though and the code above is just pseudo code.


Like @dsg but shorter. It allows you to specify what the sum should be.

public static double[] randomDoubles(int size, double sum) {
    double total=0, doubles[]=new double[size];
    for(int i=0;i<size;i++) total += doubles[i] = Math.random();
    for(int i=0;i<size;i++) doubles[i] *= sum/total;
    return doubles;
}


How about for n random numbers:

r1 = random(0,1)
r2 = random(0, 1-r1)
r3 = random(0, 1-r1-r2)
...
r(n) = 1 - r1-r2-f3...-r(n-1)
0

精彩评论

暂无评论...
验证码 换一张
取 消