Math.random()
来源:互联网 发布:淘宝靠谱的美国代购 编辑:程序博客网 时间:2024/06/07 20:33
转载自http://blog.csdn.net/fl442165035/article/details/5832823
Math.random()随机数范围
Math.random() //系统默认随机数范围 0.0~ 0.9之间的double值
/*
*注意错误使用方法
*当需要一个int型的随机数那么就(int)Math.random()这只能得到一个数 就是 0
*/
//需要int型的 0 ~ x (x为任意大于0的整数)范围的随机数
//需要0到某个数n的范围就是Math.random()*(n+1)
// 0 ~ 5之间就是乘以6
(int)(Math.random() * 6); //注意:不能写成(int)Math.random()* 5 这样将永远得到0
// 0 ~ 3之间就是乘以4
(int)(Math.random() * 4);
//需要int型的 1 ~ x(x为任意大于1的整数)范围的随机数
//需要1到某个数n的范围就是Math.random()*n + 1
// 1 ~ 5之间就是乘以5加1
(int)(Math.random() * 5 + 1); //注意:不能写成(int)Math.random()* 5 + 1这样将永远得到1
// 1 ~ 3之间就是乘以3加1
(int)(Math.random() * 3 + 1);
//如果在需要别的范围的就需要用if来筛选了
while(true){
intnum = (int)(Math.random() * 6 + 1); // 1 ~ 6 之间
if(num > 2){ //筛选出 3 ~ 6 之间的数
System.out.println(num);
}
}
- Math.random()
- Math.random()
- Math.random()
- math.random()
- math.random()
- Math.random()
- math.random()
- math.random()
- math.random()
- Math.random
- math.random()
- Math.random()
- Math.random()
- Math.random()
- Math.random()
- Math类 random()与Random()
- as3:Math.random()
- 理解Math.random()随机数
- 七天时间管理
- 一文读懂Android View事件分发机制
- git 常用命令
- Angular4自制一个市县二级联动组件
- 范数的物理意义(转)
- Math.random()
- ThreadPoolExecutor线程池的使用
- docker push失败
- nginx加密代理
- 尝试阅读ReentrantLock、AbstractQueuedSynchronizer源码(一)
- Python常用函数总结二(complex, delattr, divmod, enumerate, eval)
- 使用 PowerShell 创建 Azure VM 的自定义映像
- 查看文件时间戳命令 stat
- python完整实现发送邮件流程