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培训机构_南阳清大教育培训机构

    java培训机构_南阳清大教育培训机构正文谈到MYSQL的事物,相信大家对这几个概念都不会陌生:四大特性:ACID并发问题脏读不可重复读幻读隔离级别ReadUncommitted(读未提交)ReadCommitted(读提交)RepeatableRead(可重读)Serializable(可串行化)下面的思维导图可能更表达得清晰????MySQL的默认事物隔离级别是RR(RepeatableRead),可重复读级别是能够解决脏读、不可重复读的这两个事物并发问题的,但是幻读的问题仍

    2022年9月26日
    3
  • 转行学习3D游戏建模,你需要了解的职业分类及发展

    转行学习3D游戏建模,你需要了解的职业分类及发展王者荣耀、LOL、梦幻西游,近几年在线人数破千万,带动了越来越多的企业在游戏上的开发,3D游戏建模将游戏的画面感、真实感高度还原,给游戏者更强烈的体验感,更加身临其境。游戏模型师是目前非常热的职业岗位,目前国内动漫游戏产业已经非常成熟,需要大量优质青年加入游戏美术行业,在游戏企业里可以成为优秀的次世代场景模型师,次世代角色模型师,底模手绘贴图模型师。成功进入游戏企业之后经过项目的锻炼,薪资也会逐年有所提升。游戏建模职业分类及发展:进入游戏模型行业你可以选择不同的发展方向,比如:(1)手绘3D美术设

    2022年5月19日
    63
  • 记录mybatis分页查询失败[通俗易懂]

    记录mybatis分页查询失败[通俗易懂]在进行mybatis的分页查询时出现ExceptionInIntializerError错误代码信息如下在检查mapper和插件配置后,试着换了一下mybatis的版本后原版本替换后版本成功的进行了分页查询

    2022年6月2日
    49
  • CListCtrl控件详解

    CListCtrl控件详解Create创建列表控件并将其附加给CListCtrl对象。

    2022年6月23日
    73
  • html+JavaScript登陆注册界面

    html+JavaScript登陆注册界面最近刚刚学习了一些关于JavaScript的知识,便在之前学习的html前端的基础上做了一些简单的修改,本身还存在着很多的缺陷,希望大家多多指正。githu下载地址:https://github.com/pengxiang1998/login[登陆界面2在线预览]:https://pengxiang1998.github.io/login/index/denglu.html[注…

    2022年6月9日
    41
  • BeanUtils.populate方法使用

    BeanUtils.populate方法使用BeanUtils.populate方法使用1.在执行BeanUtils.populate之后,会把map封装成User对象。要注意的是,UserBean类中的字段名必须和html中的name属性值相同,不然在BeanUtils.populate执行之后,User对象的字段中会出现NULL数据Map<String,String[]>map=req.getParameterMap();//创建User对象UserloginUser=newUser

    2022年7月26日
    7

发表回复

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

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