JavaScript Random Number; quick random number; Math.random(); substring();

I wanted to avoid specifying a max and min.
// Math.random(): produces integer -> 0.473239055961688
// toString(): from integer to string -> '0.473239055961688'
// substring(2): extract after period -> '473239055961688'
var str = Math.random().toString().substring(2);
alert(str);
This example function requires a max and min.
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Math/random#Examples
function getRandomInt(min, max){
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
Did this help you? Please let me know with a comment. Thank you
Posted .

Comments (RSS)

Leave a comment