JavaScript: randomRange

Published on Author craigzearfossLeave a comment
/*
Returns a random integer or float in a specified range.

Examples:
    console.log(rand(2,5)); // float random between 2 and 5 inclusive
    console.log(rand(1,100,true)); // integer random between 1 and 100

reference: http://davidwalsh.name/essential-javascript-functions
*/

function rand(min, max, integer) {
    var r = Math.random() * (max - min) + min;
    return integer ? r|0 : r;
}

Leave a Reply

Your email address will not be published. Required fields are marked *