Created
October 14, 2017 10:36
-
-
Save TerryZ/67d67ed14c4d5e6388d36c5a5bc6b4a3 to your computer and use it in GitHub Desktop.
Random utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* u.random.js | |
* Random utils | |
*/ | |
var uRandom = uRandom = uRandom || {}; | |
/** | |
* Random integer value | |
* @param {Object} min | |
* @param {Object} max | |
*/ | |
uRandom.int=function(min,max){ | |
return Math.floor(min+Math.random()*((max+1)-min)); | |
} | |
/** | |
* Random float value | |
* @param {Object} min | |
* @param {Object} max | |
* @param {Object} precision | |
*/ | |
uRandom.float=function(min,max,precision){ | |
return new Number(min+Math.random()*(max-min)).toFixed(precision); | |
} | |
/** | |
* Random string | |
* @param {Object} length | |
* @param {Object} containNumber | |
* @param {boolean} upperCase | |
*/ | |
uRandom.string=function(length,containNumber,upperCase){ | |
var chars=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; | |
var target=[]; | |
var min=0,max=61; | |
if(!containNumber)min=10; | |
for(var i=0;i<length;i++){ | |
target[i]=chars[Math.floor(min+Math.random()*((max+1)-min))]; | |
} | |
return upperCase?target.join("").toUpperCase():target.join(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment