-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomDemo.java
More file actions
30 lines (23 loc) · 1.34 KB
/
Copy pathRandomDemo.java
File metadata and controls
30 lines (23 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package ABasic.A2Random;
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
Random random = new Random();
double d1 = random.nextDouble(); // 生成[0,1.0]区间的小数
double d2 = random.nextDouble() * 7; // 生成[0,7.0]区间的小数
int i1 = random.nextInt(10); // 生成[0,10]区间的整数
int i2 = random.nextInt(18) - 3; // 生成[-3,15)区间的整数
long l1 = random.nextLong(); // 生成一个随机长整型值
boolean b1 = random.nextBoolean(); // 生成一个随机布尔型值
float f1 = random.nextFloat(); // 生成一个随机浮点型值,小数点后7位
double dd1=random.nextDouble(); //生成一个随机浮点型值,小数点后16位
System.out.println("生成的[0,1.0]区间的小数是:" + d1);
System.out.println("生成的[0,7.0]区间的小数是:" + d2);
System.out.println("生成的[0,10]区间的整数是:" + i1);
System.out.println("生成的[-3,15]区间的整数是:" + i2);
System.out.println("生成一个随机长整型值:" + l1);
System.out.println("生成一个随机布尔型值:" + b1);
System.out.println("生成一个随机浮点型值:" + f1);
System.out.println("生成一个随机浮点型指:" + dd1);
}
}