• 技术文章 >Java >java教程

    java中获取不同随机数的方法

    VV2020-01-02 09:21:16原创873

    三种生成方式:

    1、通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字。

    2、通过Math.random()返回一个0到1之间的double值。

    3、通过Random类来产生一个随机数,这个是专业的Random工具类,功能强大。

    第一种:

    通过System.currentTimeMillis()来获取随机数。实际上是获取当前时间毫秒数,它是long类型。使用方法如下:

    final long l = System.currentTimeMillis();

    (免费视频教程分享:java视频教程

    若要获取int类型的整数,只需要将上面的结果转行成int类型即可。比如,获取[0, 100)之间的int整数。方法如下:

    final long l = System.currentTimeMillis();
    final int i = (int)( l % 100 );

    第二种:

    通过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);

    第三种:

    通过Random类来获取随机数。使用方法如下:

    1、创建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);

    代码示例:

     1 import java.util.Random;
     2 import java.lang.Math;
     3 
     4 /**
     5  * java 的随机数测试程序。共3种获取随机数的方法:
     6  *   (01)、通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字。
     7  *   (02)、通过Math.random()返回一个0到1之间的double值。
     8  *   (03)、通过Random类来产生一个随机数,这个是专业的Random工具类,功能强大。
     9  *
    10  * @author skywang
    11  * @email kuiwu-wang@163.com
    12  */
    13 public class RandomTest{
    14 
    15     public static void main(String args[]){
    16 
    17         // 通过System的currentTimeMillis()返回随机数
    18         testSystemTimeMillis();
    19 
    20         // 通过Math的random()返回随机数
    21         testMathRandom();
    22 
    23         // 新建“种子为1000”的Random对象,并通过该种子去测试Random的API
    24         testRandomAPIs(new Random(1000), " 1st Random(1000)");
    25         testRandomAPIs(new Random(1000), " 2nd Random(1000)");
    26         // 新建“默认种子”的Random对象,并通过该种子去测试Random的API
    27         testRandomAPIs(new Random(), " 1st Random()");
    28         testRandomAPIs(new Random(), " 2nd Random()");
    29     }
    30 
    31     /**
    32      * 返回随机数-01:测试System的currentTimeMillis()
    33      */
    34     private static void testSystemTimeMillis() {
    35         // 通过
    36         final long l = System.currentTimeMillis();
    37         // 通过l获取一个[0, 100)之间的整数
    38         final int i = (int)( l % 100 );
    39 
    40         System.out.printf("\n---- System.currentTimeMillis() ----\n l=%s i=%s\n", l, i);
    41     }
    42 
    43 
    44     /**
    45      * 返回随机数-02:测试Math的random()
    46      */
    47     private static void testMathRandom() {
    48         // 通过Math的random()函数返回一个double类型随机数,范围[0.0, 1.0)
    49         final double d = Math.random();
    50         // 通过d获取一个[0, 100)之间的整数
    51         final int i = (int)(d*100);
    52 
    53         System.out.printf("\n---- Math.random() ----\n d=%s i=%s\n", d, i);
    54     }
    55 
    56 
    57     /**
    58      * 返回随机数-03:测试Random的API
    59      */
    60     private static void testRandomAPIs(Random random, String title) {
    61         final int BUFFER_LEN = 5;
    62 
    63         // 获取随机的boolean值
    64         boolean b = random.nextBoolean();
    65         // 获取随机的数组buf[]
    66         byte[] buf = new byte[BUFFER_LEN];
    67         random.nextBytes(buf);
    68         // 获取随机的Double值,范围[0.0, 1.0)
    69         double d = random.nextDouble();
    70         // 获取随机的float值,范围[0.0, 1.0)
    71         float f = random.nextFloat();
    72         // 获取随机的int值
    73         int i1 = random.nextInt();
    74         // 获取随机的[0,100)之间的int值
    75         int i2 = random.nextInt(100);
    76         // 获取随机的高斯分布的double值
    77         double g = random.nextGaussian();
    78         // 获取随机的long值
    79         long l = random.nextLong();
    80 
    81         System.out.printf("\n---- %s ----\nb=%s, d=%s, f=%s, i1=%s, i2=%s, g=%s, l=%s, buf=[",
    82                 title, b, d, f, i1, i2, g, l);
    83         for (byte bt:buf) 
    84             System.out.printf("%s, ", bt);
    85         System.out.println("]");
    86     }
    87 }

    相关文章教程推荐:java入门教程

    以上就是java中获取不同随机数的方法的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:java 获取 随机数 方法
    上一篇:java中不能继承的类有哪些? 下一篇:requestbody和requestparam区别
    大前端线上培训班

    相关文章推荐

    • 如何将java程序打包成jar文件• java中IO流读写乱码是什么原因?• java中如何判断字符串是否存在于list集合中• java实现循环队列

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网