使用Math.floor和Math.random取随机整数
来源:互联网 发布:柏林战役 知乎 编辑:程序博客网 时间:2024/05/16 10:46
Math.random():获取0~1随机数Math.floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result. (小于等于 x,且与 x 最接近的整数。)
其实返回值就是该数的整数位:
Math.floor(0.666) --> 0
Math.floor(39.2783) --> 39
所以我们可以使用Math.floor(Math.random())去获取你想要的一个范围内的整数。
如:现在要从1~52内取一个随机数:
首先Math.random()*52 //这样我们就能得到一个 >=0 且 <52的数
然后加1:Math.random()*52 + 1 //现在这个数就 >=1 且 <53
再使用Math.floor取整
最终: Math.floor(Math.random()*52 + 1)
这就能得到一个取值范围为1~52的随机整数了.
0 0
- 使用Math.floor和Math.random取随机整数
- 使用Math.floor和Math.random取随机整数
- Math.random和Math.floor取随机数总结
- Math ceil()、floor()、round()、random()使用
- JS:Math.random(),Math.ceil(),Math.floor()与Math.round()
- Math.random()、Math.ceil()、Math.floor()、Math.round()
- javascript Math.random(),Math.floor(),Math.ceil(),Math.round()
- math.random()、math.floor()、math.round()、math.ceil()函数总结
- math.random()、math.floor()、math.round()、math.ceil()函数
- Math.ceil和Math.floor
- Math.ceil()和Math.floor()、Math.round()
- Math.round, Math.ceil 和 Math.floor
- JS中Math.ceil()和Math.floor(x)的使用
- Math.round和floor
- C#取整函数Math.Round、Math.Ceiling和Math.Floor
- <转> js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结
- C#取整函数Math.Round、Math.Ceiling和Math.Floor
- Math.floor和Math.ceil区别
- ZOJ 3396 Conference Call 最短路
- 面试题之交通灯管理系统
- MM01 BDC录屏实例
- Android 进阶学习:Android自定义View的实现方法,带你一步步深入了解View(四)
- 嵌入tomcat7
- 使用Math.floor和Math.random取随机整数
- Java基础之值传递与引用传递
- Linux进程间通信方式简介
- 5.26-5.27 最近有点闲(烦)
- 关于GridView的内容边距问题
- wince5识别U盘时间特别长的问题
- 安装sqoop2
- 备份
- 真实的