Skip to content

Instantly share code, notes, and snippets.

@Sarverott
Last active February 5, 2022 19:00
Show Gist options
  • Save Sarverott/abfa3442aa90afb97fb0fdb4e3f08025 to your computer and use it in GitHub Desktop.
Save Sarverott/abfa3442aa90afb97fb0fdb4e3f08025 to your computer and use it in GitHub Desktop.
function for generate customized random string. Works flowless in browser and with nodeJs
function randomString(len=9, matrix='qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM_-.#@'){
var ret='';
for(var i=0;i<len;i++){
ret+=matrix.charAt(Math.floor(Math.random()*matrix.length));
}
return ret;
}
function randomString(l=15, F="!".charCodeAt(0), L="~".charCodeAt(0)){
var o="";
while(l--)o+=String.fromCharCode(Math.floor(Math.random()*(L-F)+F));
return o;
}
function randomString(l=15, F="!".charCodeAt(0), L="~".charCodeAt(0)){
var a=new Array(l);
var r=function(x){
//console.log(Math.random()*(L-F)+F);
return Math.random()*(L-F)+F;
}
var o=a.fill(0).map(r);
return String.fromCharCode(...o);
}
function randomString(l=15, F="!", L="~"){
return String.fromCharCode(...Array(l).fill(0).map(function(){
return Math.random()*(L.charCodeAt(0)-F.charCodeAt(0))+F.charCodeAt(0);
}));
}
/*
EXAMPLE BEGINE
*/
console.log(randomString.v1());
console.log(randomString.v1(7));
console.log(randomString.v1(3));
console.log(randomString.v1(30));
console.log(randomString.v1(9, "1234567890"));
console.log(randomString.v1(13, ",./;'[]-=\\|}{+_:\"?><"));
console.log(randomString.v4(30, "A", "D"));
/*
EXAMPLE END
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment