获取一个随机数

157 阅读1分钟

随机点名(不重复)


        function getRandom(min, max) {
            // 返回生成一个随机的数
            return Math.floor(Math.random() * (max - min + 1)) + min
        }
        // 声明一个数组
        let arr = ['赵云', '黄忠', '关羽', '张飞', '马超', '刘备', '曹操']
            // 调用函数 返回值作为数组的索引号
        let random = getRandom(0, arr.length - 1)
            // 输出选中的名字
        console.log(arr[random])
            // 然后删除这个名字
        arr.splice(random, 1)
            // 返回删除后的数组
        console.log(arr);