<script>
// 随机数 Math.random() 随机的范围 [0 ,1)左闭右开 包括0但是没有1
console.log(Math.random()); //0 -0.999999999...
// 生成0-10的随机数 生成一个0-10的随机数一般我们要加+1,向下取整
console.log(Math.floor(Math.random() * 11)); //0-10.99999999999
// 生成0-100的随机数
// Math.floor(Math.random() * 101) //0-100.99999
// 生成5-10的随机数
// 1.生成一个0-5的随机数 ,生成完毕+5
// console.log(Math.floor(Math.random() * 6) + 5);
// 生成一个随机范围的数
function getRandom(min, max) {
// 向上取整
min = Math.floor(min);
// 向下去整
max = Math.floor(max);
// 返回一个随机数
return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
}
console.log(getRandom(1, 3));
</script>
版权声明:本文为w_meng_zhao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。