Skip to content

Instantly share code, notes, and snippets.

@Lewiscowles1986
Last active October 29, 2015 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lewiscowles1986/33f9a9475ee40a503270 to your computer and use it in GitHub Desktop.
Save Lewiscowles1986/33f9a9475ee40a503270 to your computer and use it in GitHub Desktop.
JavaScript Random Numbers
function rand( min, max ) {
var seed = Math.random();
if( typeof min != 'number' && typeof max != 'number' ) {
return seed; // default is just random
} else if( typeof min == 'number' && typeof max != 'number' ) {
return Math.round( seed * min );
} return Math.floor( seed * max ) + min;
}
//
// Test body
//
console.log = function( x ) {
document.body.innerHTML += "<pre>" + JSON.stringify( x ) + "</pre>";
}
console.log( rand() )
console.log( rand() )
console.log( rand() )
console.log( rand() )
console.log( rand() )
console.log( rand() )
console.log( rand( 5 ) )
console.log( rand( 5 ) )
console.log( rand( 5 ) )
console.log( rand( 5 ) )
console.log( rand( 5 ) )
console.log( rand( 5 ) )
console.log( rand( 1, 10 ) )
console.log( rand( 1, 10 ) )
console.log( rand( 1, 10 ) )
console.log( rand( 1, 10 ) )
console.log( rand( 1000, 9999 ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment