
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Random Value from a Range of Numbers in JavaScript
Let’s say, first, we will set the start and end range and call the function:
console.log(getRandomValueBetweenTwoValues(400, 480))
We have passed start value 400 and end value 480. Let’s get the random value with Math.random() in JavaScript −
Example
function getRandomValueBetweenTwoValues(startRange, endRange) { return Math.floor(Math.random() * (endRange - startRange + 1) + startRange); } console.log(getRandomValueBetweenTwoValues(400, 480))
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo109.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo109.js 401
Advertisements