How to get a random number between two numbers
- Previous Page JS Default Parameters
- Next Page JS Numeric Array Sorting
Learn how to get a random number between two numbers in JavaScript.
Random number
The following JavaScript function always returns a random number between the minimum (inclusive) and maximum (exclusive):
function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min) ) + min; }
The following JavaScript function always returns a random number between the minimum and maximum values (both inclusive):
function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min + 1) ) + min; }
Related Pages
Tutorial:JavaScript Random
- Previous Page JS Default Parameters
- Next Page JS Numeric Array Sorting