使用random输出10个double型的随机数

使用random输出10个double型的随机数

代码如下:

package Day05;
import java.util.Random;
import java.util.Scanner;

public class TestRandom { public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("input 2 numbers: ");
double num1 = sc.nextDouble();
double num2 = sc.nextDouble();
if (num1 != num2) {
if (num1 > num2) {
double temp = num1;
num1 = num2;
num2 = temp;
}
printTenRandoms(num1, num2);
}
} private static void printTenRandoms(double num1, double num2) {
Random rand = new Random();
double nextNumber;
for (int i = 0; i < 10; i++) {
nextNumber = num1 + (num2 - num1) * rand.nextDouble();
System.out.print(nextNumber + "\t");
}
}
}

posted @ 2017-06-20 18:53  辰峰  阅读(598)  评论(0编辑  收藏  举报