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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM