1 随机数的三种产生方式
本章先讲解Java随机数的几种产生方式,然后通过示例对其进行演示。
广义上讲,Java中的随机数的有三种产生方式:
(01). 通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字。
(02). 通过Math.random()返回一个0到1之间的double值。
(03). 通过Random类来产生一个随机数,这个是专业的Random工具类,功能强大。
1.1 第1种
通过System.currentTimeMillis()来获取随机数。实际上是获取当前时间毫秒数,它是long类型。使用方法如下:
final long l = System.currentTimeMillis();
若要获取int类型的整数,只需要将上面的结果转行成int类型即可。比如,获取[0, 100)之间的int整数。方法如下:
final long l = System.currentTimeMillis(); final int i = (int)( l % 100 );
1.2 第2种
通过Math.random()来获取随机数。实际上,它返回的是0(包含)到1(不包含)之间的double值。使用方法如下:
final double d = Math.random();
若要获取int类型的整数,只需要将上面的结果转行成int类型即可。比如,获取[0, 100)之间的int整数。方法如下:
final double d = Math.random(); final int i = (int)(d*100);
1.3 第3种
通过Random类来获取随机数。
使用方法如下:
(01) 创建Random对象。有两种方法可以创建Random对象,如下:
Random random = new Random();//默认构造方法 Random random = new Random(1000);//指定种子数字
02) 通过Random对象获取随机数。Random支持的随机值类型包括:boolean, byte, int, long, float, double。
比如,获取[0, 100)之间的int整数。方法如下
int i2 = random.nextInt(100);
Random 的函数接口
// 构造函数(一): 创建一个新的随机数生成器。 Random() // 构造函数(二): 使用单个 long 种子创建一个新随机数生成器: public Random(long seed) { setSeed(seed); } next 方法使用它来保存随机数生成器的状态。 Random(long seed) boolean nextBoolean() // 返回下一个“boolean类型”伪随机数。 void nextBytes(byte[] buf) // 生成随机字节并将其置于字节数组buf中。 double nextDouble() // 返回一个“[0.0, 1.0) 之间的double类型”的随机数。 float nextFloat() // 返回一个“[0.0, 1.0) 之间的float类型”的随机数。 int nextInt() // 返回下一个“int类型”随机数。 int nextInt(int n) // 返回一个“[0, n) 之间的int类型”的随机数。 long nextLong() // 返回下一个“long类型”随机数。 synchronized double nextGaussian() // 返回下一个“double类型”的随机数,它是呈高斯(“正常地”)分布的 double 值,其平均值是 0.0,标准偏差是 1.0。 synchronized void setSeed(long seed) // 使用单个 long 种子设置此随机数生成器的种子。
1.4 获取随机数示例
下面通过示例演示上面3种获取随机数的使用方法。
源码如下(RandomTest.java):
import java.util.Random; import java.lang.Math;/*** java 的随机数测试程序。共3种获取随机数的方法:* (01)、通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字。* (02)、通过Math.random()返回一个0到1之间的double值。* (03)、通过Random类来产生一个随机数,这个是专业的Random工具类,功能强大。** @author skywang* @email kuiwu-wang@163.com*/ public class RandomTest{public static void main(String args[]){// 通过System的currentTimeMillis()返回随机数 testSystemTimeMillis();// 通过Math的random()返回随机数 testMathRandom();// 新建“种子为1000”的Random对象,并通过该种子去测试Random的APItestRandomAPIs(new Random(1000), " 1st Random(1000)");testRandomAPIs(new Random(1000), " 2nd Random(1000)");// 新建“默认种子”的Random对象,并通过该种子去测试Random的APItestRandomAPIs(new Random(), " 1st Random()");testRandomAPIs(new Random(), " 2nd Random()");}/*** 返回随机数-01:测试System的currentTimeMillis()*/private static void testSystemTimeMillis() {// 通过final long l = System.currentTimeMillis();// 通过l获取一个[0, 100)之间的整数final int i = (int)( l % 100 );System.out.printf("\n---- System.currentTimeMillis() ----\n l=%s i=%s\n", l, i);}/*** 返回随机数-02:测试Math的random()*/private static void testMathRandom() {// 通过Math的random()函数返回一个double类型随机数,范围[0.0, 1.0)final double d = Math.random();// 通过d获取一个[0, 100)之间的整数final int i = (int)(d*100);System.out.printf("\n---- Math.random() ----\n d=%s i=%s\n", d, i);}/*** 返回随机数-03:测试Random的API*/private static void testRandomAPIs(Random random, String title) {final int BUFFER_LEN = 5;// 获取随机的boolean值boolean b = random.nextBoolean();// 获取随机的数组buf[]byte[] buf = new byte[BUFFER_LEN];random.nextBytes(buf);// 获取随机的Double值,范围[0.0, 1.0)double d = random.nextDouble();// 获取随机的float值,范围[0.0, 1.0)float f = random.nextFloat();// 获取随机的int值int i1 = random.nextInt();// 获取随机的[0,100)之间的int值int i2 = random.nextInt(100);// 获取随机的高斯分布的double值double g = random.nextGaussian();// 获取随机的long值long l = random.nextLong();System.out.printf("\n---- %s ----\nb=%s, d=%s, f=%s, i1=%s, i2=%s, g=%s, l=%s, buf=[",title, b, d, f, i1, i2, g, l);for (byte bt:buf) System.out.printf("%s, ", bt);System.out.println("]");} }