2010-11-10 110 views
2
I would like to generate random numbers between 1 & 10, with a decimal place of 1 
(e.g. 1.5, 9.4, 6.3, 2.9) 

I would like to generate random numbers between 1 & 10, with 2 decimal places 
(e.g. 1.51, 9.46, 6.32, 2.93) 

I would like to generate random numbers between 1 & 10, with 3 decimal places 

    (e.g. 1.512, 9.346, 6.329, 2.935) 

一种解决方案是将一个随机数生成器仅用于小数位,然后与数字连接。有没有更简单的方法来实现这一点?如何生成一个具有小数位的随机数

+0

尝试java.util.Random – Sid 2010-11-10 10:46:05

回答

11

虽然BigDecimal的解决方案更加灵活,这是一个使用int和double一个简单的解决方案。此方法采用一个Random对象,小数点的上界和下界和一个号码和返回一个相应格式的字符串:

/** 
* Generate a decimal string representation of a random number within the 
* supplied bounds. 
* 
* @param random 
*   the random object (if null, a new one will be created) 
* @param lowerBound 
*   the lower bound, inclusive 
* @param upperBound 
*   the upper bound, inclusive 
* @param decimalPlaces 
*   the decimal places of the result 
* @return the formatted string 
*/ 
public static String getRandomValue(final Random random, 
    final int lowerBound, 
    final int upperBound, 
    final int decimalPlaces){ 

    if(lowerBound < 0 || upperBound <= lowerBound || decimalPlaces < 0){ 
     throw new IllegalArgumentException("Put error message here"); 
    } 

    final double dbl = 
     ((random == null ? new Random() : random).nextDouble() // 
      * (upperBound - lowerBound)) 
      + lowerBound; 
    return String.format("%." + decimalPlaces + "f", dbl); 

} 

测试代码:

final Random rnd = new Random(); 

for(int decpl = 0; decpl < 3; decpl++){ 
    for(int low = 0; low < 2; low++){ 
     for(int high = low + 1; high < low + 3; high++){ 
      System.out.println("Random Value between " + low + " and " 
       + high + " with " + decpl + " decimal places:"); 
      System.out.println(getRandomValue(rnd, low, high, decpl)); 
     } 
    } 
} 

输出:

Random Value between 0 and 1 with 0 decimal places: 
0 
Random Value between 0 and 2 with 0 decimal places: 
1 
Random Value between 1 and 2 with 0 decimal places: 
1 
Random Value between 1 and 3 with 0 decimal places: 
3 
Random Value between 0 and 1 with 1 decimal places: 
0.6 
Random Value between 0 and 2 with 1 decimal places: 
1.5 
Random Value between 1 and 2 with 1 decimal places: 
1.1 
Random Value between 1 and 3 with 1 decimal places: 
1.9 
Random Value between 0 and 1 with 2 decimal places: 
0.52 
Random Value between 0 and 2 with 2 decimal places: 
0.82 
Random Value between 1 and 2 with 2 decimal places: 
1.75 
Random Value between 1 and 3 with 2 decimal places: 
1.10 
+0

与我发布的简单解决方案相比,有什么优势? – christian 2010-11-10 11:07:05

+0

它可以重复使用许多不同的参数。如果您一次使用此类功能,请按照您的方式进行。如果您多次使用它,请按照我的方式进行。 – 2010-11-10 11:15:16

3

根据您的需要小数

4

那么一个显而易见的建议是,以产生一个整数,并适当地把它生成一张双人床和比格式。因此对于3DPs,您会生成1000到9999之间的数字(或10000,取决于您想要的确切界限),然后将其除以10000.

我建议您在实际分区中使用BigDecimal,以维护小数点。

+0

当然,如果他需要结果作为一个'double',无论如何,使用BigDecimal都没有意义。 – sleske 2010-11-10 10:15:04

+0

@sleske:真的 - 但在那一点,“小数位”的想法变得有点奇怪。 – 2010-11-10 10:23:49

8

1)
(int) ((Math.random() * 90) + 10)/10.0
2)
(int) ((Math.random() * 900) + 100)/100.0
3)
(int) ((Math.random() * 9000) + 1000)/1000.0