Generate Random number in a specific range using JavaScript

   JavaScript

JavaScript has in built Math.random() method which generates the numbers randomly. Post that, using Math.floor() method you can round down the nearest integer. You can also generate random numbers within specific range.

Solution

Math.floor(Math.random() * 15);
// Returns number 0 -> 15

Math.floor(Math.random() * 15) + 1;
// Returns number 1 -> 15

Math.floor(Math.random() * 9) - 10;
// Returns number -10 -> -2
Tags: JavaScriptMath.floorMath.random

Leave a Comment