1.Math.random():获取指定范围内的随机数,速度较快准确率还可以。
//获取0 - 2之间的随机数,含头不含尾 int i = (int) 0 + (int) (Math.random() * (2 - 0));
2.new Random方式:以流的方式获取随机数,较慢
//获取0 - 2之间的随机数,含头不含尾 int i = new Random().ints(0, (2)).findFirst().getAsInt();
3.Collections.shuffle(list):这种严格意义来说不算随机数,但能起到相同的效果,他的作用是将一个list集合中的数据顺序打乱。速度较快。
//打乱整个list集合中的元素顺序 Collections.shuffle(list);