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


相关推荐

  • 怎么创建自己的网站?创建自己网站的步骤「建议收藏」

    怎么创建自己的网站?创建自己网站的步骤「建议收藏」随着互联网的不断发现,越来越多的个人站长在互联网上获得不错的流量。也有很多的企业通过自己搭建网站创建属于自己的企业网站,也获得不错的效果。在互联网风口上,如果你还不做一个网站,那真的损失太大了。为了让广大互联网工作中获利今天牛商网就为大家解答怎么创建自己的网站?创建自己网站的步骤有哪些?1、自己建站第一步:购买域名和服务器。网站的域名和服务器是一个网站的基础配件,域名就相当于你网站的地址,当别人输入你的域名的时候就可以访问你的网站。而服务器就相当于一个房子,用来存放你网站的文件和内容的。同时域名和

    2022年6月18日
    29
  • 使用ThinkPHP框架高速开发站点(多图)

    使用ThinkPHP框架高速开发站点(多图)

    2021年12月7日
    37
  • leetcode第一刷_Restore IP Addresses

    leetcode第一刷_Restore IP Addresses

    2022年2月6日
    44
  • scrollwidth和clientwidth_vue监听页面滚动

    scrollwidth和clientwidth_vue监听页面滚动本文并非原创,只是真心觉得好,特别是图解的很到位,我在js中经常会用到,就记下来,与大家分享。HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解scrollHeight:获取对象的滚动高度。scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离scrollTo

    2025年10月6日
    2
  • Hadoop实战_hadoop 项目实战

    Hadoop实战_hadoop 项目实战hadoop实战练习(二)引言:哈哈,时隔几日,坏蛋哥又回来了,继上一篇hadoop实战练习(一),坏蛋哥准备继续写一个实战练习实例。苏格拉底曾说:所有科学都源于需求。那么我们就抛出今天实战项目的需求:百度采集了一段时间用户的访问日志。需要将数据进行清洗变成结构化的数据,方便后面模型或报表的制作。那么就让我们开始吧!码字不易,如果大家想持续获得大数据相关内容,请关注和点赞坏蛋哥(haha…)文章目录:文章目录hadoop实战练习(二)一项目需求分析二项目实现思路三具体实现代码讲解

    2025年6月7日
    3
  • Docker 下查看Redis版本的命令「建议收藏」

    Docker 下查看Redis版本的命令「建议收藏」命令:dockerexec-itfirst-redisredis-server-vps:其中first-redis为redis在docker中的容器名称

    2022年6月3日
    165

发表回复

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

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