随机数、函数封装、某个范围内的随机数

318 阅读1分钟

随机数 封装


        // min - max 的随机数(整数)
        function getRandom(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min
        }
        getRandom(a, b)    //调用函数

 例子:    
        Math.random()的取值范围为[0,1),
        5 - 10的随机数
        0 - 5的随机数 + 5
        console.log(Math.floor(Math.random() * (10-5 + 1)) + 5);  //[0,6)+5 --> [5-11) 取5-10的随机数