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] 之间的随机整数。
  • 相关阅读:
    看了下swift,一如既往的蛋疼。
    运算符重载 C++ 编程思想
    OPENGL学习笔记整理(五):着色语言
    [ZJOI2009]对称的正方形 manacher+单调队列
    sam板子
    模拟41
    P1640 [SCOI2010]连续攻击游戏
    Dp搬运工3
    noip2018 赛道修建
    P3224 [HNOI2012]永无乡
  • 原文地址:https://www.cnblogs.com/eaysun/p/4385801.html
Copyright © 2011-2022 走看看