Math对象的随机方法

 

 

<script>
    // 求两个数之间的随机整数
    function getdom(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
    }
    console.log(getdom(1, 20));
    // 随机点名
    var arr = ["张三", "张三丰", "李虎", "王五", "李四"];
    console.log(arr[getdom(0, arr.length - 1)]);
  </script>

 

posted @ 2022-04-13 09:09  罗砂  阅读(31)  评论(0)    收藏  举报