js随机数
JS脚本 2023/5/25 19:19:44 点击:不统计
js随机数,即javascript 生成随机的数字,也可以通过字母索引僧传随机字母。
看一下 js随机数实现方式,在javascript中 Math.random() 是生成 0-0.999无限循环的小于1的数。
看一下 js随机数实现方式,在javascript中 Math.random() 是生成 0-0.999无限循环的小于1的数。
那随机整数的产生,js随机数直接看下面代码:
<script>
//js生成 0-9.x(<10) 随机小数
const jsRandom = Math.random() * 10;
document.write(jsRandom);
//js生成 0-9(<10) 随机整数,Math.floor为向下取整
const jsRandom2 = Math.floor(Math.random() * 10);
document.write(jsRandom2);
//js生成 1-10(<11) 随机整数,Math.ceil 向上取整
const jsRandom3 = Math.ceil(Math.random() * 10);
document.write(jsRandom3);
//js生成 从20-25 (包含20,不包含25)随机整数
const jsRandom4 = Math.floor(Math.random() * (25 - 20)) + 20;
document.write(jsRandom4);
//js生成 从20-25 (包含20,包含25)随机整数
const jsRandom5 = Math.floor(Math.random() * (26 - 20)) + 20;
document.write(jsRandom5);
</script>