Was it helpful?

Question


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
raja
Published on 09-Sep-2020 13:00:25

Was it helpful?
Not affiliated with Tutorialspoint
scroll top