Java Random nextInt()方法与示例[通俗易懂]
2023-06-13 09:12:39 时间
大家好,又见面了,我是你们的朋友全栈君。
随机类nextInt()方法 (Random Class nextInt() method)
Syntax:
句法:
public int nextInt();
public int nextInt(int num);
- nextInt() method is available in java.util package. nextInt()方法在java.util包中可用。
- nextInt() method is used to return the next pseudo-random value from this Random Value Generator. nextInt()方法用于从此随机值生成器返回下一个伪随机值。
- nextInt(int num) method is used to return the next pseudo-random distribute integer value between 0 and the given parameter (num) from this Random Generator. nextInt(int num)方法用于从此随机数生成器返回下一个介于0和给定参数(num)之间的下一个伪随机分布整数值。
- These methods may throw an exception at the time of returning the next integer value. 这些方法在返回下一个整数值时可能会引发异常。 IllegalArgumentException: This exception may throw when the given parameter (num<0) is invalid. IllegalArgumentException :当给定参数(num <0)无效时,可能引发此异常。
- These are non-static methods and it is accessible with the class object and if we try to access these methods with the class name then also we will get any error. 这些是非静态方法,可通过类对象进行访问,如果尝试使用类名访问这些方法,则也会遇到任何错误。
Parameter(s):
参数:
- In the first case, nextInt()
在第一种情况下, nextInt()
- It does not accept any parameter.
- In the second case, nextInt(int num)
在第二种情况下, nextInt(int num)
- int num – represents the last endpoint of this Random Value Generator.
- int num –表示此随机值生成器的最后一个端点。
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
相关文章
- java long string 转换_Java long 转成 String的实现[通俗易懂]
- java工具类-Java对象转换成Map
- 反应java程序并行机制的特点_Java语言具有许多优点和特点,下列选项中能反映Java程序并行机制特点的是()。…[通俗易懂]
- java中文乱码_Java中文乱码问题的解决方案[通俗易懂]
- java redis锁_Java中Redis锁的实现[通俗易懂]
- 手机版java编译器_Java编译器[通俗易懂]
- java 随机数算法_Java随机数算法原理与实现方法实例详解
- java url加密_Java实现url加密处理的方法示例
- java 常量表达式,需要常量表达式? (Java switch语句)[通俗易懂]
- java查找字符的方法_Java字符串查找(3种方法)
- Java 中static和非static的区别(方法和变量)
- Control character in cookie value, consider BASE64 encoding your value , java操作cookie遇到中文会报错的解决方案详解编程语言
- Java Map.keySet()方法:获取Map集合的所有键名
- Java网站在Linux上的运行(java网站linux)
- 处理表题:基于Redis与Java的过期处理方法研究(redisjava过期)
- 时间Java中设置Redis Key过期时间的方法(redisjava过期)
- 数据处理基于Redis和Java的过期数据处理方法(redisjava过期)
- Java与Redis的事务使用方法浅析(javaredis事务)
- Java Redis实例学习与应用(java redis实例)
- 缓存使用Redis让Java代码更加迅速缓存设置(redis设置java)
- 安卓应用开发通过java调用c++jni的图文使用方法