已知最大最小值为
double max = 22.0;
double min = 11.0;
其实我想产生比min小1比max大1范围的浮点数,即 [10,23].
如何利用min和max去获得满足条件的随机数?求各位大哥指点指点,如果方便最好给个实例。
------解决思路----------------------
package test;
import java.util.Random;
public class Test {
public static void main(String[] args) throws Exception {
double max = 22.0;
double min = 11.0;
System.out.println(nextDouble(min - 1, max + 1));
}
public static double nextDouble(final double min, final double max) throws Exception {
if (max < min) {
throw new Exception("min < max");
}
if (min == max) {
return min;
}
return min + ((max - min) * new Random().nextDouble());
}
}
------解决思路----------------------
import org.apache.commons.math.random.RandomData;
import org.apache.commons.math.random.RandomDataImpl;
public void method( ) {
RandomData randomData = new RandomDataImpl( );
int number = randomData.nextInt(5,10);