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.


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);
   return Math.max(...res);
console.log(pickGreatestRandom(range, count));


And the output in the console will be −


Updated on: 20-Nov-2020


Kickstart Your Career

Get certified by completing the course

Get Started