Math.random() in ECMAScript 262

Return a floating-point random number between 0 (inclusive) and 1 (exclusive).

Return Type


The following code sample shows a convenience method which extends the Math object to support an addition method, allowing the caller to specify minimum and maximum values, and to specify if the result should be an integer or a floating point number:

Math.randomValue = function (min,max,asFloat){
    var r = Math.random()*(max-min)+min;
    return asFloat?r:Math.round(r);

var percentChance = Math.randomValue(0,100,true); // e.g. 98.76234112334
var fromOneToTen = Math.randomValue(1,10); // e.g. 8

Math.random() will never return 1, but Math.round() is called on the result for integer values; as such, percentChance might be 99.9999 but it will never be 100, whereas fromOneToTen might easily be 10.