摘要:测试Random生成随机数的随机性
import java.util.*;
public class RandomTest {
public static void main(String[] args) {
Random rand = new Random(47);
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < 1000; i++) {
int r = rand.nextInt(30);
Integer value = map.get(r);
// System.out.print(value+" ");
map.put(r, value == null ? 1 : value + 1);ge
}
// System.out.println();
System.out.println(map);
}
}
说明:Key为Random生成的随机数,Value为数字出现的次数。
如果键不在容器中,get()方法返回NULL(表示第一次被找到);否则,返回与键相关联的值。
运行结果:{0=31, 1=36, 2=35, 3=34, 4=36, 5=34, 6=28, 7=36, 8=38, 9=44, 10=32, 11=33, 12=38, 13=32, 14=34, 15=33, 16=28, 17=30, 18=28, 19=31, 20=37, 21=30, 22=28, 23=32, 24=26, 25=36, 26=39, 27=37, 28=31, 29=33}