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


相关推荐

  • java xml与实体类转换「建议收藏」

    java xml与实体类转换「建议收藏」@XmlRootElement注解定义xml的根节点通过name改变根节点的xml值          @XmlRootElement(name=”amimal_test”)@XmlType注解指定序列生成的xml节点顺序         @XmlType(propOrder={“id”,”age”,”name”,”bir

    2022年7月21日
    12
  • c#程序中的AssemblyInfo.cs

    c#程序中的AssemblyInfo.cs在asp.net中有一个配置文件AssemblyInfo.cs主要用来设定生成的有关程序集的常规信息dll文件的一些参数,下面是默认的AssemblyInfo.cs文件的内容具体介绍//是否符合公共语言规范(CLS)[assembly:CLSCompliant(true)]//控制程序集中所有类型对COM的可访问性[assembly:ComVisible(false)]//代码的作者和这…

    2022年7月13日
    15
  • Vue(4)Vue指令的学习1

    Vue(4)Vue指令的学习1前言Vue官网一共有提供了14个指令,分别如下v-textv-htmlv-showv-if☆☆☆v-else☆☆☆v-else-if☆☆☆v-for☆☆☆v-on☆☆☆v

    2022年7月29日
    2
  • 短信定位 android,短信定位

    短信定位 android,短信定位开发者名称:xunl这是一款利用发送短信来得到对方位置的应用.应用场景:1.寻找家人:家里有小孩子或老人,只要给对方发送一条包含设置好关键字的短信内容,即可以地图的方式在当前手机上显示对方的位置.2.找回手机:自己的手机找不到了,只要给自己的手机发送一条短信,即可得到手机所在地理位置.使用方法(寻找家人):1.首先说一下概念:你想知道谁的位置,那么谁就是软件中的TA,而你在软件中的角色是主人.2….

    2022年10月13日
    0
  • cubieboard mysql_Cubieboard 安装Nand系统,配置nginx,php,mysql,samba详细教程

    cubieboard mysql_Cubieboard 安装Nand系统,配置nginx,php,mysql,samba详细教程安装前置条件1.下载win32diskimager-v0.7-binary.zip2.下载将系统安装到Nand1将TF卡装入读卡器,插入电脑USB接口.2.解压win32diskimager,debian_wheezy_armhf_v1_mele到当前目录,执行Win32DiskImager.exe.3.选择debian_wheezy_armhf_v1_mele.img,盘符选择你读卡器的盘符,点…

    2022年7月22日
    4
  • java 远程debug_idea如何debug

    java 远程debug_idea如何debug使用IDEA远程Debug线上服务应用背景配置过程IDEA配置服务启动配置应用方法注意事项应用背景通常情况下我们会遇到只有线上环境才能复现的bug,此时通过在代码里面加日志重新发布,反复定位对线上的客户体验极度不好,此时我们可以使用IDEA的远程Debug功能,对线上bug调试。配置过程该过程需要本地环境和线上环境至少保证指定端口互通,该端口指的是线上debug对项目的监听端口。IDEA配置首先在IDEA上进行配置,进入项目启动面板,Edit-config中设置点击”+“号选中”Remo

    2022年9月3日
    3

发表回复

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

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