在范围之间生成n个随机数,并在JavaScript中选择最大的数
我们需要编写一个JavaScript函数,该函数采用两个数字组成的数组作为第一个参数,该数组指定一个可以生成随机数的数字范围。
第二个参数是一个数字,它指定了我们必须生成的随机数。
然后,最后我们的函数应该返回最大的所有随机数。
示例
为此的代码将是-
const range = [15, 26];
const count = 10;
const randomBetweenRange = ([min, max]) => {
const random = Math.random() * (max - min) + min;
return random;
};
const pickGreatestRandom = (range, count) => {
const res = [];
for(let i = 0; i < count; i++){
const random = randomBetweenRange(range);
res.push(random);
};
return Math.max(...res);
};
console.log(pickGreatestRandom(range, count));输出结果
控制台中的输出将是-
25.686387806628826
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短