js Math [ 随机数、绝对值、四舍五入、进一取整、舍去取整、最大值、最小值、圆周率 ]
<script>
/*
数学对象:Math
*/
with (document) {
write('<br>-3.5的绝对值:'+Math.abs(-3.5));
write('<br>3.5的四舍五入:'+Math.round(3.01));
write('<br>3.01的进一取整:'+Math.ceil(3.01));
write('<br>3.99的舍去取整:'+Math.floor(3.99));
write('<br>获取最大值:'+Math.max(10,20,45,12));
write('<br>获取最大值:'+Math.min(10,20,45,12));
write('<br>获取圆周率'+Math.PI); // Math.random():获取的>=0 <1的随机数
write('<br>获取随机数'+Math.random()); /*
// 0-10的随机数
Math.round(Math.random()*10);
0-0.5 0
0.5-1.5 1
1.5-2.5 2 9.5-10 10
*/
// 0 - 10的随机数
console.log(Math.ceil(Math.random()*100000)%11); // 10-50的随机数
/*
0-40
+10
*/
console.log(Math.ceil(Math.random()*100000)%41+10); // m-n的随机数
function getRandom(m, n){
return Math.ceil(Math.random()*100000)%(n-m+1)+m;
} console.log(getRandom(2,3));
}
</script>
js Math [ 随机数、绝对值、四舍五入、进一取整、舍去取整、最大值、最小值、圆周率 ]的更多相关文章
- js 获取随机数 Math.random()
js 获取随机数 Math.random() // 结果为0-1间的一个随机数(包括0,不包括1) var randomNum1 = Math.random(); //console.log(rand ...
- JS 取整、取余
一.取整 1. 取整 // 丢弃小数部分,保留整数部分 parseInt(7/2) // 3 2. 向上取整 // 向上取整,有小数就整数部分加1 Math.ceil(7/2) // 4 3. 向下取 ...
- JS基础入门篇(三)— for循环,取余,取整。
1.for循环 1.for的基本简介 作用: 根据一定的条件,重复地执行一行或多行代码 语法: for( 初始化 ; 判断条件 ; 条件改变 ){ 代码块 } 2.for循环的执行顺序 <bod ...
- [JS]Math.random()
参考网址:http://www.soulteary.com/2014/07/05/js-math-random-trick.html [JS]Math.random()的二三事 看到题目,如果大家平时 ...
- js Math 对象的方法
Math对象与JavaScript其他对象不同,因为用户不能生成所使用对象的副本,相反脚本汇集了简单Math对象的属性和方法.Math对象实际上存在于每个窗口和框架中,但是对脚本没有影响,程序员把这种 ...
- js获取随机数
js 获取随机数方法如下: 1.Math.random()表示 结果为0-1间的一个随机数(包括0,不包括1) : 返回指定范围的随机数(m-n之间)的公式 Math.random()*(n-m)+m ...
- js取整数、取余数
js取整数.取余数 取整 1.取整 // 丢弃小数部分,保留整数部分 parseInt(5/2) // 2 2.向上取整 // 向上取整,有小数就整数部分加1 Math.ceil(5/2) // 3 ...
- js Math对象的常用方法
1,基本方法: Math.round();向上四舍五入. Math.ceil();向上取整,有小数就整数部分加1 Math.floor(5/2) ;向下取整 Math.abs();返回绝对值: Mat ...
- 真的了解js生成随机数吗
由js生成一切随机数的基础都是Math.random()这个方法,这个方法比较特别,生成的随机数落在的区间是[0,1),进行一次操作的话,js只能生成一个类似于[n,m)这样,左闭右开的区间.所以当有 ...
随机推荐
- Codeforces Round #273 (Div. 2)-A. Initial Bet
http://codeforces.com/contest/478/problem/A A. Initial Bet time limit per test 1 second memory limit ...
- Luogu P2397 yyy loves Maths VI (mode)
题目传送门 虽然只是一道黄题,但还是学到了一点新知识-- 摩尔投票法 用\(O(1)\)的内存,\(O(n)\)的时间来找出一串长度为n的数中的众数,前提是众数出现的次数要大于\(n/2\) 方法很简 ...
- initWithNibName:bundle awakeFromNib 区别
initWithNibName:bundle 定义:is a message sent to a view (or window) controller in order to create the ...
- 摘抄 Promise原理
1.简单的promise: //极简promise雏形 function Promise(fn){ var value = null; callbacks = [];//callback为数组,因为可 ...
- verilog behavioral modeling ---Block statements
block statements : 1. sequential block : begin-end block 2.parallel block : fork - join bloc ...
- The US in understimating Huawei, says founder Ren zhengfei
Huawei Founder Ren Zhengfei has downplayed the impact of the US executive order that cripple Huawei' ...
- Python中摘要算法MD5,SHA1讲解
摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符串表示).摘要算法就是通过摘要函数f()对任意长度的数据data计算出固定长度的摘要di ...
- Java-使用哈希码比较对象的值
在一些特殊的情况下使用 package com.tj; import java.io.File; public class MyHash { public static void main(Strin ...
- luogu1231 教辅的组成
注意把书拆成两份 #include <iostream> #include <cstring> #include <cstdio> #include <que ...
- Android开发调试无法连接到夜神模拟器的解决方法
Android开发调试无法连接到夜神模拟器的解决方法: 一般原因是adb的版本不一致造成的!!!!!换成一样的就可以了. 在网上看到的方法,特记录下来: 1.任务管理器里看下,adb.exe以及nox ...