Generate n random numbers between a range and pick the greatest in JavaScript


We are required to write a JavaScript function that takes in an array of two numbers as the first argument, this array specifies a number range within which we can generate random numbers.

The second argument will be a single number that specifies the count of random numbers we have to generate.

Then at last our function should return the greatest all random numbers generated.

Example

The code for this will be −

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));

Output

And the output in the console will be −

25.686387806628826

Updated on: 20-Nov-2020

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements