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


相关推荐

  • 安装并使用EVE模拟器

    安装并使用EVE模拟器本文提供的软件及相关镜像有:VMWareEVECommunityVMversion2.0.3-95Wireshark-win64-2.6.4CiscoIOL镜像(路由器、交换机)CRTWindowsXP镜像安装步骤如下:1.进行VMWare的安装a.在安装包文件夹内选择“VMware-workstation-full-15.0.0-10134415”的应用程…

    2022年6月12日
    40
  • Java打印带边框的乘法口诀表_Java 面试 – 打印九九乘法口诀表

    Java打印带边框的乘法口诀表_Java 面试 – 打印九九乘法口诀表在Java面试过程中,面试者经常会被要求手写代码或上机操作。一般来说,手写代码或上机操作,主要还是考察面试者的分析问题和解决问题的能力。打印九九乘法口诀无疑是非常基础的,那么如何实现呢?首先我们先来分析一下九九乘法口诀表内在的规律,然后再根据分析结果,选择适合的解决方案。九九乘法口诀表:1*1=11*2=22*2=41*3=32*3=63*3=91…

    2022年7月15日
    14
  • MFC进度条编程控制[通俗易懂]

    MFC进度条编程控制[通俗易懂]好文章转来转去图片也找不到了 最原版的链接也打不开了 比较遗憾\*****************************************************************************************************************************************************************

    2022年7月12日
    15
  • MySQL删除表提示Cannot truncate a table referenced in a foreign key constraint解决办法

    MySQL删除表提示Cannot truncate a table referenced in a foreign key constraint解决办法背景因为测试过程中,几套环境都是用的同一个库,数据有点冲突,需要删库。执行truncatetablexxx时提示:[Err]1701-Cannottruncateatablereferencedinaforeignkeyconstraint….解决办法删除之前先执行删除外键约束SETforeign_key_checks=0删除完之后再执行启动外

    2022年6月26日
    41
  • MessageBox()功能

    MessageBox()功能

    2022年1月1日
    59
  • Win10 快捷键大全(史上最全)「建议收藏」

    Win10 快捷键大全(史上最全)「建议收藏」windows10常用快捷键。win10正式版是微软续已发布的Windows系统的最新版操作系统。windows10(win10正式版)让人感到最意外的就是直接跳过了win9。那么今天我为大家讲解他推出的常用快捷键。希望能够帮到大家。复制、粘贴和其他常规键盘快捷方式按此键执行此操作Ctrl+X剪切选定项Ctrl+C(或Ctrl+Insert)复制选…

    2022年5月30日
    33

发表回复

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

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