在 JavaScript 中生成某个范围内的 n 个随机数并选取最大值

javascriptweb developmentfront end technologyobject oriented programming

我们需要编写一个 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

相关文章


有用资源