Java随机数

来源:互联网 发布:linux运维项目 编辑:程序博客网 时间:2024/06/10 11:08

广义上讲,Java中的随机数的有三种产生方式:
1. 通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字。
2. 通过Math.random()返回一个0到1之间的double值。
3. 通过Random类来产生一个随机数,这个是专业的Random工具类,功能强大。

第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 );

第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);

第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 种子设置此随机数生成器的种子。

获取随机数示例

import java.util.Random;import java.lang.Math;/** * java 的随机数测试程序。共3种获取随机数的方法: *   1、通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字。 *   2、通过Math.random()返回一个0到1之间的double值。 *   3、通过Random类来产生一个随机数,这个是专业的Random工具类,功能强大。 * * @author sunkun * @email sunkun201309@163.com */public class RandomTest{    public static void main(String args[]){        // 通过System的currentTimeMillis()返回随机数        testSystemTimeMillis();        // 通过Math的random()返回随机数        testMathRandom();        // 新建“种子为1000”的Random对象,并通过该种子去测试Random的API        testRandomAPIs(new Random(1000), " 1st Random(1000)");        testRandomAPIs(new Random(1000), " 2nd Random(1000)");        // 新建“默认种子”的Random对象,并通过该种子去测试Random的API        testRandomAPIs(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("]");    }}