import java.util.Random;
/*
- Random: 伪随机数类
- 构造:
-
public Random() 创建一个默认种子的随机数对象,而这个默认种子在世界上是独一无二的
-
public Random(long seed) 创建一个指定种子的随机数对象,相同的种子,随机数是固定的。
- 普通方法:
-
1:public double nextDouble() [0,1)
-
2:public int nextInt(int n) [0,n) 通过该方法可以求任意 m~n区间的随机数: random.nextInt(n-m) + m
*/ public class Demo02_Random {
public static void main(String[] args) {
Random random = new Random();
//1:public double nextDouble() [0,1)
double nextDouble = random.nextDouble();
// System.out.println(nextDouble);
//2:public int nextInt(int n) [0,n)
for (int i = 0; i < 100; i++) {
int nextInt = random.nextInt(100);
// System.out.println(nextInt); }
//随机数的种子演示,相同的种子,随机数是固定的。?
Random random2 = new Random(123456);
// Random random2 = new Random();
for (int i = 0; i < 100; i++) {
int nextInt = random2.nextInt();
System.out.println(nextInt);
}
}
}