Random 類的 nextInt() 方法從隨機數生成器的序列中返回下一個偽隨機、均勻分布的 int 值。
用法
public int nextInt()
參數
NA
返回值
nextInt() 方法返回從隨機數生成器的序列中抽取的下一個偽隨機 int 值。
例子1
import java.util.Random;
public class JavaRandomNextIntExample1 {
public static void main(String[] args) {
Random random = new Random();
//return the next pseudorandom integer value
Integer val = random.nextInt();
System.out.println("Random Integer value:"+val);
Integer val1 = random.nextInt();
System.out.println("Random Integer value:"+val1);
}
}
輸出:
Random Integer value:-298063488 Random Integer value:1400961289
Java 隨機 nextInt() 方法
Random 類的 nextInt(int n) 方法返回一個介於零(包含)和指定值(不包含)之間的偽隨機 int 值,從隨機數生成器的序列中抽取。
用法
public int nextInt(int n)
參數
n:它是要返回的隨機數的界限。它必須是積極的。
返回值
nextInt() 方法返回從隨機數生成器的序列中抽取的介於 0 和 n 之間的下一個偽隨機 int 值。
拋出
如果 n 不是正數,則 nextInt() 方法拋出 IllegalArgumentException。
例子2
import java.util.Random;
public class JavaRandomNextIntExample2 {
static int j=1;
public static void main(String[] args) {
Random random = new Random();
for (int i=1;i<8;i++) {
//return the next pseudorandom Integer value
Integer val =random.nextInt(i);
System.out.println(j++ +". Random intvalue:"+val+" ");
}
}
}
輸出:
1. Random intvalue:0 2. Random intvalue:1 3. Random intvalue:0 4. Random intvalue:3 5. Random intvalue:1 6. Random intvalue:0 7. Random intvalue:0
例子3
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
public class JavaRandomNextIntExample3 {
static int j=1;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of players.");
int capacity = scanner.nextInt();
ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(capacity);
System.out.println("Enter name of " + capacity + " Players.");
for (int i = 0; i < capacity; i++) {
System.out.print(j++ + " ");
String str = scanner.next();
queue.add(str);
}
j = 1;
Random random = new Random();
for (String xyz:queue) {
Integer val = random.nextInt(capacity);
System.out.println(j++ +". " +xyz+" will have "+val+" bonus points.");
}
}
}
輸出:
Enter number of players.3 Enter name of 3 Players. 1 Sukla 2 Mukta 3 Amar 1. Sukla will have 0 bonus points. 2. Mukta will have 1 bonus points. 3. Amar will have 2 bonus points.
示例 4
import java.util.Random;
public class JavaRandomNextIntExample4 {
public static void main(String[] args) {
Random random = new Random();
//return the next pseudorandom integer value
Integer val = random.nextInt(0);
System.out.println("Random Integer value:"+val);
}
}
輸出:
Exception in thread "main" java.lang.IllegalArgumentException:bound must be positive atjava.util.Random.nextInt(Random.java:388) at com.javaTpoint.JavaRandomNextIntExample4.main(JavaRandomNextIntExample4.java:9)
注意:綁定值必須是正數,否則會給出 IllegalArgumentException。
相關用法
- Java Random nextInt()用法及代碼示例
- Java Random nextDouble()用法及代碼示例
- Java Random nextGaussian()用法及代碼示例
- Java Random nextBoolean()用法及代碼示例
- Java Random nextBytes()用法及代碼示例
- Java Random nextFloat()用法及代碼示例
- Java Random next()用法及代碼示例
- Java Random nextLong()用法及代碼示例
- Java Random setSeed()用法及代碼示例
- Java Random doubles()用法及代碼示例
- Java RandomAccessFile close()用法及代碼示例
- Java RandomAccessFile readByte()用法及代碼示例
- Java RandomAccessFile readChar()用法及代碼示例
- Java RandomAccessFile writeUTF()用法及代碼示例
- Java RandomAccessFile readUnsignedByte()用法及代碼示例
- Java RandomAccessFile writeFloat()用法及代碼示例
- Java RandomAccessFile getFD()用法及代碼示例
- Java RandomAccessFile writeChar()用法及代碼示例
- Java RandomAccessFile writeBytes()用法及代碼示例
- Java RandomAccessFile readInt()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Random nextInt() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。