A:Random类的概述
此类用于产生随机数如果用相同的种子创建两个 Random 实例,
则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
B:构造方法
public Random() 没有给定种子,使用的是默认的(当前系统的毫秒值)
public Random(long seed) 给定一个long类型的种子,给定以后每一次生成的随机数是相同的
C:成员方法
public int nextInt()//没有参数 表示的随机数范围 是int类型的范围
public int nextInt(int n)//可以指定一个随机数范围
void nextBytes(byte[] bytes) 生成随机字节并将其置于用户提供的空的 byte 数组中。
public class RandomDemo {
public static void main(String[] args) {
// Random 可以生成一些随机数据
// Random()
// 创建一个新的随机数生成器。
Random random = new Random();
//int nextInt ()
//返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。
//int nextInt ( int n)
//返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
byte[] bytes = new byte[10];
// void nextBytes ( byte[] bytes)
// 生成随机字节并将其置于用户提供的 byte 数组中。
random.nextBytes(bytes);
System.out.println(Arrays.toString(bytes));
//for (int i = 0; i < 200; i++) {
// //int num = random.nextInt();//生成随机整数,不给范围,他用的是int的范围
// int num = random.nextInt(100)+1; //0----99
// //double num = random.nextDouble();
// // boolean b = random.nextBoolean();
// System.out.println(num);
//}
}
}
public class RandomDemo2 {
public static void main(String[] args) {
//Random( long seed)
//使用单个 long 种子创建一个新的随机数生成器。
Random random = new Random(1L);
for (int i = 0; i < 10; i++) {
int i1 = random.nextInt(10);
System.out.println(i1);
}
}
}