美文网首页
JS Math.random()方法取得指定范围内的随机数

JS Math.random()方法取得指定范围内的随机数

作者: Gino_Li | 来源:发表于2019-02-23 23:45 被阅读0次

    公式:

    Math.floor(Math.random()*(max-min+1)+min);
    // max:最大值    min:最小值
    //注:如果不包含最大值无需+1
    

    如[5,23)即5到23且不包括23的整数

    var ran = Math.floor(Math.random()*(23-5)+5);
    

    相关文章

      网友评论

          本文标题:JS Math.random()方法取得指定范围内的随机数

          本文链接:https://www.haomeiwen.com/subject/fxkmyqtx.html