Generating a random number between two values using javascript

Generating a random number pure javascript

// REQUIRED
function getRandom(x, y) {
    return Math.floor(Math.random() * (y - x + 1)) + x
}

// Print
var print = function (val) {
    document.body.innerHTML = document.body.innerHTML + "<br />" + val
}
// Function Call
print(getRandom(0, 5))
print(getRandom(-10, 10))
print(getRandom(50, 80))

print(getRandom(-8, 0))
print(getRandom(-15, -10))
print(getRandom(-60, -30))
DEMO
Generating a random number between two values using javascript Generating a random number between two values using javascript Reviewed by Bhaumik Patel on 7:19 PM Rating: 5