大家好,又见面了,我是你们的朋友全栈君。
Syntax:
句法:
public int nextInt();
public int nextInt(int num);
Parameter(s):
参数:
Return value:
返回值:
In both the cases, the return type of the method is int – it returns next pseudorandom distributed value between 0 and num.
在这两种情况下,方法的返回类型均为int –它返回0至num之间的下一个伪随机分布值。
Example:
例:
// Java program to demonstrate the example
// of nextInt() method of Random
import java.util.*;
public class NextIntOfRandom {
public static void main(String args[]) {
// Instantiates Random object
Random ran = new Random();
// By using nextInt() method is
// to return next int pseudo-random
// value by using Random Value Generator
int val = ran.nextInt();
// Display val
System.out.println("ran.nextInt(): " + val);
// By using nextInt(int) method is
// to return next int pseudo-random
// value between 0 and the given value
// and 0 is inclusive whereas the given value
// is exclusive by using Random Value Generator
val = ran.nextInt(50);
// Display val
System.out.println("ran.nextInt(50): " + val);
}
}
Output
输出量
RUN 1:
ran.nextInt(): -1450643138
ran.nextInt(50): 13
RUN 2:
ran.nextInt(): 1448295644
ran.nextInt(50): 47
RUN 3:
ran.nextInt(): 397396236
ran.nextInt(50): 11
翻译自: https://www.includehelp.com/java/random-nextint-method-with-example.aspx
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/162910.html原文链接:https://javaforall.cn