java Random()用法

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

posted on 2021-09-18 17:04  风中明月  阅读(1980)  评论(0编辑  收藏  举报