- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements