JS Math 类库介绍
来源:互联网 发布:java gc finalize 编辑:程序博客网 时间:2024/06/05 17:18
下面介绍下随机生成数的常用几个API
JS 随机数生成 : 在JavaScript , 提供了生成随机数的API, Math.random()
1、Math.random() : 随机生成小数 。 生成数区间(0, 1)
2、Math.ceil(param) :小数向上取整.
3、Math.floor(param) : 小数向下取整
4、Math.round(param) : 小数四舍五入
灵活运用 : 如何随机生成整数
eg : 随机生成10之间的整数
Math.round(Math.random() * 10)
代码如下 :
<!DOCTYPE html><html><head><meta charset="utf-8" /></head><body> <button onclick="random()">随机小数 (0, 1)</button> <button onclick="random2()">随机数, 向上取整数 (0, 10)</button> <button onclick="random3()">随机数, 向下取整数 (0, 10)</button> <button onclick="random4()">随机数, 四舍五入整数 (0, 10)</button> </body><script> function random() { console.log(Math.random()); } function random2() { r = Math.random() * 10; console.log(); console.log("原值 = " + r + ", ceil = " + Math.ceil(r)); } function random3() { r = Math.random() * 10; console.log(); console.log("原值 = " + r + ", floor = " + Math.floor(r)); } function random4() { r = Math.random() * 10; console.log(); console.log("原值 = " + r + ", round = " + Math.round(r)); } </script></html>
0 0
- JS Math 类库介绍
- JS Math 类库介绍
- js Math工具类
- js中Math对象的介绍
- java.math.BigDecimal类介绍
- Math.js库的使用
- java 之Math库类的使用简单介绍
- JS Math
- js math
- js Math
- <Js>Math
- Math 类中BigDecimal的介绍
- 前端数字运算库Math.js
- JS:Math.random(),Math.ceil(),Math.floor()与Math.round()
- 【原创】开源Math.NET基础数学类库使用(01)综合介绍
- 开源Math.NET基础数学类库使用(01)综合介绍
- javascript Math 对象介绍
- Math方法详细介绍
- 位优化快速幂板子
- vim 打开多个文件
- 洛谷1880 石子合并
- C# 过滤html标签
- RPM包制作方法
- JS Math 类库介绍
- 洛谷3379 最近公共祖先模板(倍增)
- [前端JS学习笔记]JavaScript 数组
- 欧拉筛板子
- [前端JS学习笔记]JavaScript CallBack
- 约数和问题 (codevs2606 && 洛谷2424)
- 蓝桥杯java第八届第二题--纸牌三角形
- [前端JS学习笔记]JavaScript function
- 如何把选择屏幕放到标准屏幕上