java Random.nextInt()方法

java Random.nextInt()方法publicintnextInt(intn)该方法的作用是生成一个随机的int值,该值介于[0,n)的区间,也就是0到n之间的随机int值,包含0而不包含n。直接上代码:

大家好,又见面了,我是你们的朋友全栈君。

public int nextInt(int n)

该方法的作用是生成一个随机的int值,该值介于[0,n)的区间,也就是0到n之间的随机int值,包含0而不包含n。

 

直接上代码:

package org.xiaowu.random.demo;

import java.util.Random;

import org.junit.Test;

public class RandomDemo {
    
    @Test
    public void Demo(){
        Random rnd = new Random();
        int code = rnd.nextInt(8999) + 1000;
        System.out.println("code:"+code);
    }

    
    @Test
    public void Demo1(){
        Random r = new Random();
        int nextInt = r.nextInt();
        Random r1 = new Random(10);
        int nextInt2 = r1.nextInt();
        System.out.println("nextInt:"+nextInt);
        System.out.println("nextInt2:"+nextInt2);
    }

    
    /**
     * 生成[0,1.0)区间的小数
     * 
     */
    @Test
    public void Demo2(){
        Random r = new Random();
        double d1 = r.nextDouble();
        System.out.println("d1:"+d1);
    }

    
    /**
     * 生成[0,5.0)区间的小数
     * 
     */
    @Test
    public void Demo3(){
        Random r = new Random();
        double d2 = r.nextDouble()* 5;
        System.out.println("d1:"+d2);
    }

    
    /**
     * 生成[1,2.5)区间的小数
     * 
     */
    @Test
    public void Demo4(){
        Random r = new Random();
        double d3 = r.nextDouble() * 1.5 + 1;
        System.out.println("d1:"+d3);
    }

    
    /**
     * 生成任意整数
     * 
     */
    @Test
    public void Demo5(){
        Random r = new Random();
        int n1 = r.nextInt();
        System.out.println("d1:"+n1);
    }

    
    
    /**
     * 生成[0,10)区间的整数
     * 
     */
    @Test
    public void Demo6(){
        Random r = new Random();
        int n2 = r.nextInt(10);
        int n3 = Math.abs(r.nextInt() % 10);
        System.out.println("n2:"+n2);
        System.out.println("n3:"+n3);
    }

    
    
    /**
     * 生成[0,10]区间的整数
     * 
     */
    @Test
    public void Demo7(){
        Random r = new Random();
        int n3 = r.nextInt(11);
        int n4 = Math.abs(r.nextInt() % 11);
        System.out.println("n3:"+n3);
        System.out.println("n4:"+n4);
    }

    
    /**
     * 生成[-3,15)区间的整数
     * 
     */
    @Test
    public void Demo8(){
        Random r = new Random();
        int n4 = r.nextInt(18) - 3;
        int n5 = Math.abs(r.nextInt() % 18) - 3;
        System.out.println("n4:"+n4);
        System.out.println("n5:"+n5);
    }
    
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/154967.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vue md5.js_VUE.js

    vue md5.js_VUE.js<template><div><mavonEditorv-model=”markdown”:codeStyle=”codeStyle”></mavonEditor><divv-html=”compiledMarkdown”></div></div></template><script>//编辑import{mavonEditor}from’.

    2022年9月2日
    3
  • mshta usage

    mshta usagemshtaisshortforMicroSoftHtmlApplication.Itcouldrunhtmlfileorhtmlstringasaparameter.whatisinterestingisyoucanuseitinawindowbatchcommandorbatchfileandyoucanusesome

    2022年7月15日
    25
  • python 获取时间戳_python将日期转换成时间戳

    python 获取时间戳_python将日期转换成时间戳1、获取秒级、毫秒级和微秒级时间戳importtimeimportdatetimet=time.time()#当前时间print(t)#原始时间数据print(int(t))#秒级时间戳print(int(round(t*1000)))#毫秒级时间戳print(int(round(t*1000000)))#微秒级时间戳结果:1634191096.03610181634191096163419109603616341910960361

    2025年8月30日
    7
  • 02.pycharm中配置PyInstaller打包工具

    02.pycharm中配置PyInstaller打包工具我用的环境版本python解释器:3.6.6pycharm开发工具:2018.3.6社区版PyInstaller打包工具:4.5.1pycharm中配置PyInstaller打包工具opts可选的参数参数含义-F-onefile,打包成一个exe文件-D-onefile,创建一个目录,包含exe文件,但会依赖很多文件(默认选项)-c-console,-nowindowed,使用控制台,无窗口(默认)-w-Windowed,-noconsole,使用窗

    2025年7月7日
    4
  • 浏览器被hao360劫持解决办法

    浏览器被hao360劫持解决办法浏览器被hao360劫持怎么办chromeedge被hao360劫持解决办法建议chromeedge被hao360劫持chrome和edge被hao360劫持后的样子解决办法在该目录下C:\ProgramData\Microsoft\Windows\StartMenu\Programs删除被hao360修改快捷方式(edge为例chrome我已删除了)最后浏览器回复正常。建议安装软件到对应官网下载正规软件安装…

    2022年7月26日
    22
  • python_sklearn库的使用

    python_sklearn库的使用sklearn库的使用

    2022年10月17日
    3

发表回复

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

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