当前位置:网站首页>3.2-随机数
3.2-随机数
2022-07-28 16:35:00 【night_du】
Java当中获取随机数的方法:
- 使用Random对象完成随机数的获取
指定区间: min ~ max
int类型整数 nextInt(max - min) + min
double类型浮点数 nextDouble() * (max - min) + min
Random random = new Random();
//获取0-10之间的一个随机数
// 从0-10的范围中获取一个随机数 int类型的 包含0,但是不包括10
int num = random.nextInt(10);
//获取10-30之间的随机整数
//获取指定区间的随机数: min ~ max nextInt(max - min) + min
int num1 = random.nextInt(30 - 10) + 10;
//获取浮点类型的数据
double num3 = random.nextDouble(); // 0-1 之间的浮点数据
//获取指定区间的浮点类型数据
//0 ~ 10 之间的
double num4 = random.nextDouble() * (10 - 0) + 0;
//获取20 - 50 之间的浮点数据
double num5 = random.nextDouble() * (50 - 20) + 20;
- 使用Math中的random方法完成
本身的结果就是一个0 - 1 之间的浮点类型数据
指定区间: min ~ max
double类型浮点数据: Math.random() * (max - min) + min
// 0-1 之间的浮点类型的数据
double n = Math.random();
//获取指定区间的浮点类型的数据
double n1 = Math.random() * (50 - 20) + 20;
边栏推荐
猜你喜欢

MySQL optimization summary

leetcode系统性刷题(二)------贪心、回溯、递归

QT programming serial port assistant

Please make sure you have the correct access rights and the repository exists.

An article takes you closer to the overview and principle of kubernetes

三维点云处理系列----PCA

都说软件测试是IT行业最差的,是这样的吗?

2021 National Undergraduate data statistics and Analysis Competition

Public medical database

【p5.js学习笔记】码绘的基础知识
随机推荐
新人如何入门学习软件测试
电脑充不进去电的解决方法
[阅读笔记] For Paper:R-CNN系列的三篇论文总结
【C语言进阶】——剖析入微数据在内存中的存储 【下】(浮点数存储)
Talk about what you know about publishing online (2)
.net MVC understanding
Openpcd安装过程记录
leetcode系统性刷题(二)------贪心、回溯、递归
[C language note sharing] character function and string function (recommended Collection)
QT编写串口助手
Can‘t use an undefined value as an ARRAY reference at probe2symbol
.net MVC的理解
PCA reports error in eigen (crossprod (t (x), t (x)), symmetric = true): 'x' has infinite value or missing value
Public medical database
电工学自学笔记1.22
abstract、static、final
内部类、常用类
[C language must see] yo, writing bugs, I'm sure you've stepped on the pit
Visual object class introduces Pascal VOC dataset
软件测试的培训机构靠谱吗