random生成[x,y]之间的随机整数

本文最后更新于:2 年前

random生成 [x, y]之间的随机整数

1
2
3
4
5
6
7
8
9
<script>
/*
* 生成两个整数之间的随机整数,并且要包含这两个整数
*/
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandom(1, 10));
</script>

随机点名

1
2
3
4
5
6
7
8
9
10
11
12
13
<script>
/*
* 生成两个整数之间的随机整数,并且要包含这两个整数
*/
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

const arr = ['许嵩', '邓紫棋', '毛不易', '解忧邵帅'];
const index = getRandom(0, arr.length - 1); // 生成随机的index
console.log(arr[index]); // 随机点名

</script>