0

I have created a method to create some random double values for example 10 values : num1, num2, …num10 which sum of this 10 values is 1 : num1+num2+…+num10 = 1 My method is like the method in forum
Getting N random numbers that the sum is M :

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; 
} 

But this method create very long numbers like: 0.18593711849349975 I even used Math.round() method but with this method my numbers are 0.0, 0.5, 0.0 , … I need numbers from 0.01 to 0.99 or 0.1 to 0.9. If I was using integer numbers I could do this with something like Random.nextInt(0.98) +0.01 , but nextDouble() method doesn’t accept parameters, how can I do this? Would you please help me? thanks

Community
  • 1
  • 1
Elton.fd
  • 1,575
  • 3
  • 17
  • 24
  • I'd suggest you reconsider the title for this question, to make it more meaningful. Also, reconsider some of your variable names. 'n' and 'm' don't mean a lot to the reader. – MSpeed Dec 10 '10 at 12:32

4 Answers4

4

You could generate integers via nextInt, and then divide them by the required power of 10.

Valentin Rocher
  • 11,667
  • 45
  • 59
2

Generate nextDouble(); and round it using this method

public static double round(double d, int decimalPlace){
    BigDecimal bd = new BigDecimal(Double.toString(d));
    bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
  }  
     double d = 3.1537;
      // output is 3.2
      System.out.println(d + " : " + round(d, 1));
      // output is 3.15
      System.out.println(d + " : " + round(d, 2));
      // output is 3.154
      System.out.println(d + " : " + round(d, 3));
jmj
  • 237,923
  • 42
  • 401
  • 438
  • I used your method, it works nice, but after using it, the sum of numbers won't be 1 – Elton.fd Dec 10 '10 at 15:14
  • @Sandra You can random no with precision using this method and then simply can apply basic arithmetic to find filling no that will make sum 10 – jmj Dec 10 '10 at 15:48
1

Multiply your number by 100 and round the result. That will give you the original number with the insignificant digits stripped off. Then subsequently divide the result by 100 to get back to the original scale.

x = Math.round(x*100.0) / 100.0;
mmccomb
  • 13,516
  • 5
  • 39
  • 46
0

If you want to use 2 decimals, you could do it as follows:

private static double[] randSum(int n, double m){
    double[] randoms = new double[n];
        int valueLeft = (int) (m * 100);

        for(int i = 0; i < n-1; i++){
             int tempRand = (int)(Math.random() * valueLeft);

             randoms[i] = (double)tempRand / 100;
             valueLeft -= tempRand;

             System.out.println(tempRand + " " + randoms[i] + " " + valueLeft);
        }
        randoms[n-1] = (double)valueLeft/100;

        return randoms;
}
13Tazer31
  • 181
  • 3