1.random.nextInt()

random.nextIn()的作用是随机生成一个int类型,因为int 的取值范围是 -2147483648——2147483647 ,所以生成的数也是处于这个范围。

2.random.nextInt(int bound)

random.nextInt(int bound)方法的作用是生成一个0-参数bound范围内的随机数,但是要记住,参数bound必须是正数,不可为负数,否则在运行时会报java.lang.IllegalArgumentException: bound must be positive的错误,提示bound必须是正数,下面看用法:

Random random = new Random();
System.out.println("int:"+random.nextInt(20));

输出:
int:12
3.random.nextLong()

random.nextLong()会随机生成一个Long类型,同理,因为Long的取值范围是 -9223372036854775808——9223372036854775807,所以也会生成一个这个区间的数。

Random random = new Random();
System.out.println("Long:"+random.nextLong());
System.out.println("Long.MIN-Long.MAX:"+Long.MIN_VALUE+"-"+Long.MAX_VALUE);

输出:
Long:-5059225360401714325
Long.MIN-Long.MAX:-9223372036854775808-9223372036854775807

    1
    2
    3
    4
    5
    6
    7

4.random.nextDouble()

random.nextDouble()会生成一个0-1的double类型,而不是生成double取值范围中的数,下附取值范围,就不多说了。

Random random = new Random();
System.out.println("double:"+random.nextDouble());
System.out.println("Double.MIN-Double.MAX:"+Double.MIN_VALUE+"-"+Double.MAX_VALUE);

输出:
double:0.9059561641891956
Double.MIN-Double.MAX:4.9E-324-1.7976931348623157E308

    1
    2
    3
    4
    5
    6
    7

在输出double的取值范围时,我们会发现是4.9E-324-1.7976931348623157E308,会有一个大写字母E+数字的组合,这就是科学计数法,E代表10,后面跟多少数字,就代表是10的多少次方,如下:

System.out.println("double E1 = "+4.9E1);
System.out.println("double E2 = "+4.9E2);
System.out.println("double E3 = "+4.9E3);
System.out.println("double E4 = "+4.9E4);
System.out.println("double E-1 = "+4.9E-1);
System.out.println("double E-2 = "+4.9E-2);
System.out.println("double E-3 = "+4.9E-3);
System.out.println("double E-4 = "+4.9E-4);

输出:
double E1 = 49.0
double E2 = 490.0
double E3 = 4900.0
double E4 = 49000.0
double E-1 = 0.49
double E-2 = 0.049
double E-3 = 0.0049
double E-4 = 4.9E-4

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

在科学技术法中,当E后是正数时,只有从E7,也就是10的7次方开始才使用科学计数法表示,在E后是负数时,在E-4,也就是10的-4次方开始使用科学计数法表示。

5.random.nextFloat()

random.nextFloat()会生成一个随机的0-1之间的浮点型,大体同double一样,下附取值范围。

Random random = new Random();
System.out.println("float:"+random.nextFloat());
System.out.println("Float.MIN-Float.MAX:"+Float.MIN_VALUE+"-"+Float.MAX_VALUE);

输出:
float:0.56538385
Float.MIN-Float.MAX:1.4E-45-3.4028235E38

    1
    2
    3
    4
    5
    6
    7

6.random.nextBoolean()

random.nextBoolean()会生成一个true或false,这个想必就不用多说了。

Random random = new Random();
System.out.println("boolean:"+random.nextBoolean());

输出:
boolean:false

    1
    2
    3
    4
    5

7.random.nextBytes(byte[] bytes)

random.nextBytes()会为一个byte类型的数组随机赋值,具体如下所示:

Random random = new Random();

byte[] bytes = new byte[5];

random.nextBytes(bytes);
for (int i = 0; i < bytes.length; i++) {
    byte aByte = bytes[i];
    System.out.print(aByte+"\n");
}

输出:
25
43
75
-84
-36

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

因为byte的取值范围为 -128到127,所以也就是说会为一个byte类型的数组在-128,127这个区间内重新随机赋值,此处“重新随机赋值”划重点,也就是说,即使原本的byte数组里面有值,那么也会重新覆盖掉,看下面的例子:

Random random = new Random();

byte[] bytes = {1,2,3,4,5};

random.nextBytes(bytes);
for (int i = 0; i < bytes.length; i++) {
    byte aByte = bytes[i];
    System.out.print(aByte+"\n");
}

输出:
15
82
-67
74
72
————————————————

原文链接:https://blog.csdn.net/qq_39754721/article/details/94736251

最新文章

  1. WebService的创建发布及部署
  2. MyBatis dao层 方法传参
  3. WP老杨解迷:评论数和下载量、榜单的关系
  4. mialx配置qq邮箱发送邮件
  5. 压力测试工具——Galting
  6. Metrics.NET report to Zabbix
  7. Spring MVC Checkbox And Checkboxes Example
  8. Floyd-Warshall算法的理解
  9. 什么是SysWow64
  10. Linux内存点滴:用户进程内存空间
  11. Qt事件机制浅析(定义,产生,异步事件循环,转发,与信号的区别。感觉QT事件与Delphi的事件一致,而信号则与Windows消息一致)
  12. JavaScript中的execCommand()命令详解及实例展示
  13. thinkphp 3.2 模型的使用示例
  14. 1002: [FJOI2007]轮状病毒
  15. mac清除某个端口的占用
  16. json格式处理及扩展
  17. 小程序中data数据的处理方法总结(小程序交流群:604788754)
  18. Opencv基本数据类型
  19. redis 安装 与错误解决办法
  20. 三星vs苹果 2018Q3 财报 以及国内最赚钱的公司...

热门文章

  1. https的加密解密过程
  2. 手把手搭建自己的智能家居 - 基于 IOT Pi 的智能甲醛检测器
  3. PCIE学习链接集合
  4. 六. Go并发编程--WaitGroup
  5. Typora使用教程
  6. 『学了就忘』Linux基础命令 — 27、搜索操作相关命令
  7. Redis源码分析(adlist)
  8. robot_framewok自动化测试--(6)Collections 库
  9. appium环境搭建基于安卓(mac系统)
  10. maven项目中 把依赖包打进jar包