Java Random nextInt()方法与示例[通俗易懂]

Java Random nextInt()方法与示例[通俗易懂]随机类nextInt()方法(RandomClassnextInt()method)Syntax:句法:publicintnextInt();publicintnextInt(intnum);nextInt()methodisavailableinjava.utilpackage.nextInt()方法在java.util包中可…

大家好,又见面了,我是你们的朋友全栈君。

随机类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.net/162910.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月22日 下午9:00
下一篇 2022年7月22日 下午9:16


相关推荐

  • 黑盒测试 场景法_黑盒测试方法定义

    黑盒测试 场景法_黑盒测试方法定义通过运用场景来对系统的功能点或业务流程的描述,从而提高测试效果。场景法一般包含基本流和备用流,从一个流程开始,通过描述经过的路径来确定的过程,经过遍历所有的基本流和备用流来完成整个场景。  为什么场景法能如此清晰的描述整个事件?因为,现在的系统基本上都是由事件来触发控制流程的。如:我们申请一个项目,需先提交审批单据,再由部门经理审批,审核通过后由总经理来最终审批,如果部门经理审核不通过

    2026年4月15日
    6
  • Windows下搭建FTP服务器的一些总结

    Windows下搭建FTP服务器的一些总结Windows下搭建FTP服务器前言:如果你的电脑上的控制面板–程序–打开或关闭windows功能–windows功能里面没有互联网信息服务(IIS),那就别在网上浪费时间了,我找着了一天的资料打了各种补丁没成功,没有相关的IIS或者连c:\windows\system32\inetsrv这个路径都没有,原因是因为你的电脑系统可能是家庭版win7,或者是ghost安装系统的时候精简…

    2022年7月21日
    22
  • NOIP 2012 文化之旅 题解[通俗易懂]

    NOIP 2012 文化之旅 题解[通俗易懂]来水一篇题解,我看洛谷上说的这道题的数据特别水,于是就写了很水的做法。题目:P1078[NOIP2012普及组]文化之旅-洛谷|计算机科学教育新生态(luogu.com.cn)题目背景本题是错题,后来被证明没有靠谱的多项式复杂度的做法。测试数据非常的水,各种玄学做法都可以通过(比如反着扫),不代表算法正确。因此本题题目和数据仅供参考。题目描述有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超过一次(即如果他学习了某种文化,则他就不能到达其

    2022年8月22日
    7
  • 02324 离散数学 知识点

    02324 离散数学 知识点第 1 章命题逻辑

    2026年1月26日
    4
  • 敏捷测试的理解

    敏捷测试的理解首先敏捷测试 Agiletesting 是测试的一种 原有测试定义中通过执行被测系统发现问题 通过测试这种活动能够提供对被测系统提供度量等概念还是适用的 敏捷测试是遵循敏捷宣言的一种测试实践 1 强调从客户的角度 即从使用系统的用户角度 来测试系统 2 重点关注持续迭代地测试新开发的功能 而不再强调传统测试过程中严格的测试阶段 3 建议尽早开始测试 一旦系统某个层面可测 比如提供了模块功能 就要

    2025年8月28日
    4
  • 信号与系统公式笔记(8)——拉普拉斯变换

    信号与系统公式笔记(8)——拉普拉斯变换这里是关于第 5 章的内容 拉普拉斯变换 基本内容 1 拉普拉斯变换定义 收敛域 2 拉普拉斯变换的性质 和傅里叶变换类似 3 拉普拉斯反变换 4 拉普拉斯变换与电路分析 5 系统函数 6 拉普拉斯变换与傅里叶变换关系对不符合狄利克雷条件的函数无法做傅里叶变换 所以搞出来个拉普拉斯变换 用 eSteSt mathrm e St 的例子

    2026年3月19日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号