Last active
August 12, 2020 04:09
-
-
Save socmia/db6a69ad290fe4a476a019ad60586011 to your computer and use it in GitHub Desktop.
Using javascript unique Generate Id
This file contains hidden or 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 generateId () { | |
const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-'; | |
var chars = []; | |
for (var i = 0; i < 7; i++) { | |
chars[i] = charset[Math.floor(Math.random() * charset.length)] | |
} | |
return chars.join(''); | |
} | |
// Java | |
public static String generateId() { | |
String charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"; | |
StringBuilder s = new StringBuilder(); | |
for (int i = 0; i < 7; i++) { | |
s.append(charset.charAt((int) (Math.floor(Math.random() * charset.length())))); | |
} | |
return s.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment