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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • css模糊遮罩效果_CSS 半透明遮罩层「建议收藏」

    css模糊遮罩效果_CSS 半透明遮罩层「建议收藏」很多时候,我们需要通过一层半透明的遮罩层来把后面的一切整体调暗,以便凸显某个特定的UI元素,引导用户关注,比如弹出层或者交互指南。这个效果的传统方法就是增加一个额外的HTML元素用于遮挡背景添加如下样式:.overlay{position:fixed;top:0;right:0;bottom:0;left:0;background-color:rgba(0,0,0,.8);}.lig…

    2022年7月13日
    104
  • 常用分子生物学实验技术–整理「建议收藏」

    常用分子生物学实验技术–整理「建议收藏」常用的分子生物学实验技术:离心技术:是分离纯化蛋白质、酶、核酸(DNA、RNA)、细胞的最常用方法之一。电泳(electrophoresis):带电粒子在电场中向着与其所带电荷相反方向电极移动的

    2022年7月4日
    32
  • 拉姆达表达式的使用[通俗易懂]

    拉姆达表达式的使用[通俗易懂]1.什么是λ表达式 λ表达式本质上是一个匿名方法。让我们来看下面这个例子:   publicintadd(intx,inty){       returnx+y;   }转成λ表达式后是这个样子:       (intx,inty)-&gt;x+y;参数类型也可以省略,Java编译器会根据上下文推断出来:   (x,y)…

    2022年9月19日
    1
  • ICMP 协议「建议收藏」

    ICMP 协议「建议收藏」一、什么是ICMP协议?ICMP(InternetControlMessageProtocol)Internet控制报文协议。它是TCP/IP协议簇的一个子协议,用于在IP主机、路由器之间传递控制消息。控制消息是指网络通不通、主机是否可达、路由是否可用等网络本身的消息。这些控制消息虽然并不传输用户数据,但是对于用户数据的传递起着重要的作用。ICMP使用IP的基本支持,就像它是一个更高级别…

    2025年8月13日
    2
  • 细谈Type-C、PD原理(一)[通俗易懂]

    细谈Type-C、PD原理(一)[通俗易懂]第一部分包含:一、二、三、四第二部分包含:五、六目录一、Type-C简介以及历史二、Type-CPort的DataRole、PowerRole三、Type-C的Data/PowerRole识别协商/AltMode四、如何进行数据链路的切换五、相关参数/名词/状态解释六、PD协议简介一、Type-C简介以及历史自1998年…

    2025年5月30日
    2
  • python 操作es

    python 操作es

    2021年11月27日
    58

发表回复

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

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