Last active
February 5, 2022 19:00
-
-
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
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
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; | |
} |
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
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; | |
} |
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
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); | |
} |
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
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); | |
})); | |
} |
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
/* | |
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