zoukankan      html  css  js  c++  java
  • JavaScript 学习笔记——Math属性及其方法

    Math.random() — 返回0和1之间的伪随机数 可能为0,但总是小于1,[0,1)。

    Math.random()*10//返回 0-10 之间的随机数。
    Math.random()*(20-10)+10 //返回10-20之间的随机数。
    Math.random()*(n-m)+m //返回(m-n)之间的随机数。 

    Math.floor() -- 向下取得一个最接近的整数

    Math.floor(12.2)// 返回12 
    Math.floor(12.7)//返回12 
    Math.floor(12.0)//返回1

    Math.ceil() -- 向上取得一个最接近的整数

    Math.ceil(12.2)//返回13 
    Math.ceil(12.7)//返回13 
    Math.ceil(12.0)// 返回12

    Math.round() -- 进行四舍五入

    Math.round(12.2)// 返回12 
    Math.round(12.7)//返回13 
    Math.round(12.0)//返回12

    这些函数平常实际使用中大多相互配合:例如要想生成 0 - 10 之间的随机整数,可以这样写

    Math.floor(Math.random()*10);
    //其中Math.random()*10返回 [0 - 10)之间的随机数,再利用Math.floor(),只取出其中的整数。
    
    Math.ceil(Math.random()*10);
    //那么依据上述介绍,这样因为Math.ceil()向上取整,那么则返回 [1 - 10] 之间的随机整数。
  • 相关阅读:
    使用Silverlight 实现工作流流程定义
    中国国内航线信息的可视化
    笨办法学R编程(5)
    笨办法学R编程(4)
    笨办法学R编程(3)
    香山杯部分WP
    PWNABLE 3x17
    PWNABLE dubblesort
    PWNABLE applestore
    pyc文件修复出题经历
  • 原文地址:https://www.cnblogs.com/eaysun/p/4385801.html
Copyright © 2011-2022 走看看