Skip to content

Instantly share code, notes, and snippets.

@Elijah-trillionz
Last active April 11, 2021 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elijah-trillionz/25ed2b79fe56f16cf404190f0116add7 to your computer and use it in GitHub Desktop.
Save Elijah-trillionz/25ed2b79fe56f16cf404190f0116add7 to your computer and use it in GitHub Desktop.
How to randomly generate strong ids for project.
const letter = 'abcdefghijklmnopqrstuvwxyz';
const allCharacters = `${letter}1234567890123456789123456789$&@*£€¥%${letter.toUpperCase()}`;
const allCharactersInArray = allCharacters.split('');
function randomise() {
const randomCharacter =
allCharactersInArray[Math.floor(Math.random() * allCharactersInArray.length)];
return randomCharacter;
}
function generateId() {
const suggest = [];
for (let i = 0; i < 16; i++) {
suggest.push(randomise());
}
return suggest.join(''); // will generate a very strong id
}
console.log(generateId())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment