前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java random.nextint_java Random.nextInt()方法的具体使用

java random.nextint_java Random.nextInt()方法的具体使用

作者头像
全栈程序员站长
发布2022-09-15 10:04:20
3630
发布2022-09-15 10:04:20
举报
文章被收录于专栏:全栈程序员必看

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

lic 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);

}

}

Java中使用Random类中的nextInt()方法返回一个伪随机数

问题

今天想让程序返回一个区间内的随机数。忘记怎么写了,就去百度搜给出的结果并不对。

import java.util.Random;

/**

* @author HP

* @date 2019/4/16

*/

public class randomTest {

public static void main(String[] args) {

Random random = new Random();

//生成64-128内的随机数

int i = random.nextInt() * (128 – 64 + 1) + 64;

/**

* 生成 [m,n] 的数字

* int i1 = random.nextInt() * (n-m+1)+m;

* */

//生成0-64内的数字

int i1 = random.nextInt() * (64-0+1);

/**

* 生成0-n之内的数字

* int i1 = random.nextInt() * (n-0+1);

*

*

* */

}

}

这样是不对的,我就去查看API文档,发现nextInt()可以有参数也可以无参数。

无参数的方法直接调用返回的是一个很大的正负区间上的数。

如果想返回想要的范围内的数,应该:

package chapter6;

import java.util.Random;

import org.omg.Messaging.SyncScopeHelper;

public class RandomTest {

public static void main(String[] args) {

Random random = new Random();

for(int i=0;i<200;i++)

{

// 输出为0~13之间的整数

System.out.println(random.nextInt(14));

}

System.out.println(“—————————-“);

for(int j=0;j<100;j++){

// 输出为-9~0之间的整数

System.out.println(random.nextInt(10)-9);

}

}

}

到此这篇关于java Random.nextInt()方法的具体使用的文章就介绍到这了,更多相关java Random.nextInt使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档