随机数

Math.random()  生成[0, 1)范围内的double类型随机数 。线程安全,多线程环境能被调用。

 (int)(Math.random()*n)  生成  [0 , n) 之间的随机整数。 比如 (int)(Math.random()*100)  生成 [ 0, 100 ) 之间的随机整数。

如无特殊说明,使用 (int)(Math.random()*n)  即可。

 

 

random.nextInt(n)  生成 [0 , n) 之间的伪随机整数 , 其中 参数 n 必须是 正整数,如果是负整数,则会报 : java.lang.IllegalArgumentException 

 

伪随机数: 相同种子数的Random对象生成的随机数序列相同。 

System.out.println("a: " +  random1.nextInt(2000) + "==" + "b:" + random2.nextInt(2000));   a 和 b 总是相同的。

posted @ 2016-11-17 17:43  zhoudingzhao  阅读(261)  评论(0编辑  收藏  举报