zoukankan      html  css  js  c++  java
  • 生成n~m的随机数

    生成n-m,包含n但不包含m的整数:

    第一步算出 m-n的值,假设等于w

    第二步Math.random()*w

    第三步Math.random()*w+n

    第四步parseInt(Math.random()*w+n, 10)

    生成n-m,不包含n但包含m的整数:​

    第一步算出 m-n的值,假设等于w

    第二步Math.random()*w

    第三步Math.random()*w+n

    第四步Math.floor(Math.random()*w+n) + 1

    生成n-m,不包含n和m的整数:

    第一步算出 m-n-2的值,假设等于w

    第二步Math.random()*w

    第三步Math.random()*w+n +1

    第四步Math.round(Math.random()*w+n+1) 或者 Math.ceil(Math.random()*w+n+1)

    生成n-m,包含n和m的随机数:

    第一步算出 m-n的值,假设等于w

    第二步Math.random()*w

    第三步Math.random()*w+n

    第四步Math.round(Math.random()*w+n) 或者 Math.ceil(Math.random()*w+n)

    例子:

      生成800-1500的随机整数,包含800但不包含1500

    1. 1500-800 = 700
    2. Math.random()*700
    3. var num = Math.random()*700 + 800;
    4. num = parseInt(num, 10);

    只需要简单的四步就可以完成。

    补充:

      Math.ceil()      返回大于等于数字参数的最小整数(取整函数),对数字进行上舍入

      Math.floor()    返回小于等于数字参数的最大整数,对数字进行下舍入

      Math.round()  返回数字最接近的整数,四舍五入

  • 相关阅读:
    jmeter察看结果树中文乱码解决办法
    使用postman测试接口
    使用Jenkins持续集成
    Python单元测试unittest测试框架
    使用类封装一个签名规则
    Python处理URL编码
    Python中的 if __name__ == '__main__' 是什么意思?
    python发送邮件
    instruction 寻址,sib modrm
    .sv 与.svh之间的区别
  • 原文地址:https://www.cnblogs.com/lk516924/p/4037307.html
Copyright © 2011-2022 走看看