Skip to content

Instantly share code, notes, and snippets.

@JDMcKinstry
Created February 25, 2016 20:48
Show Gist options
  • Save JDMcKinstry/8ce644700d2eba5568f9 to your computer and use it in GitHub Desktop.
Save JDMcKinstry/8ce644700d2eba5568f9 to your computer and use it in GitHub Desktop.
JS Easy way to create Random String
if (Object['defineProperty'] && !String.hasOwnProperty('rand')) Object.defineProperty(String,"rand",{value:function(){function f(a){for(var c=(Math.random()*eval("1e"+~~(50*Math.random()+50))).toString(36).split(""),b=3;b<c.length;b++)b==~~(Math.random()*b)+1&&c[b].match(/[a-z]/)&&(c[b]=c[b].toUpperCase());c=c.join("");c=c.substr(~~(Math.random()*~~(c.length/3)),~~(Math.random()*(c.length-~~(c.length/3*2)+1))+~~(c.length/3*2));if(24>a)return a?c.substr(c,a):c;c=c.substr(c,a);if(c.length==a)return c;for(;c.length<a;)c+=f();return c.substr(0,a)}var d=arguments,a,e;if(!d.length)return f();for(var b=0;b<d.length;b++)"string"==typeof d[b]&&d[b].length&&!a&&(a=d[b]),"number"==typeof d[b]&&d[b]&&!e&&(e=d[b]);if(!a&&!e)return f();if(!a)return f(e);if(!e){a=window.btoa(escape(encodeURIComponent(a))).replace(/[^\w]/g,"");a=a.split("");for(b=a.length-1;0<b;b--){var d=Math.floor(Math.random()*(b+1)),g=a[b];a[b]=a[d];a[d]=g}return a.join("")}a=window.btoa(escape(encodeURIComponent(a))).replace(/[^\w]/g,"");b=f(e-a.length);a=(a+b).split("");for(b=a.length-1;0<b;b--)d=Math.floor(Math.random()*(b+1)),g=a[b],a[b]=a[d],a[d]=g;a=a.join("");return a.length==e?a:a.substr(0,e)}});
// Use as simple as
var str = String.rand();
// Accepts 2 parameters, order of no matter
// Param STRING If provided, will encode the string and use letters and numbers for encoding for generating random number
// If length is longer than encoding, then it will be salted with other random characters
// Param INT If provided, determines total length of the return String. Quite accurate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment