<
<script>
// 1.Math对象随机数方法 random()返回一个随机的小数 0 =< x < 1
// 2.我们想要得到两个数之间的随机整数 并且 包含这2个整数
// Math.floor(Math.random()*(max-min+1))+min;
function getRandom(min,max){
return Math.floor(Math.random()*(max - min +1)) + min;
}
console.log(getRandom(1,10));
// 3.例子 随机点名
var arr =['张三','张三丰','李四','王五','红红'];
console.log(arr[getRandom(0,arr.length - 1)]);
</script>
Math对象——利用Math.random()对象实现随机点名
于 2022-05-13 13:07:34 首次发布