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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。