Skip to content

Instantly share code, notes, and snippets.

@roshanca
Created January 16, 2018 03:49
Show Gist options
  • Save roshanca/04c51b175de8abf8ae20c623d47dda34 to your computer and use it in GitHub Desktop.
Save roshanca/04c51b175de8abf8ae20c623d47dda34 to your computer and use it in GitHub Desktop.
生成指定位数的随机数
/**
* 生成指定位数的随机数
* @param {number} x
*/
export const randomStr = (x) => {
let s = "";
while(s.length < x && x > 0){
let v = Math.random() < 0.5 ? 32 : 0;
s += String.fromCharCode(Math.round(Math.random() * ((122 - v) - (97 - v)) + (97 - v)));
}
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment