Skip to content

Instantly share code, notes, and snippets.

@belchior
Created October 27, 2017 22:04
Show Gist options
  • Save belchior/f373af162842db7ca088cfd479cddf93 to your computer and use it in GitHub Desktop.
Save belchior/f373af162842db7ca088cfd479cddf93 to your computer and use it in GitHub Desktop.
generate a random hash between 1 and 10 chars
function randomHash(numberOfChars) {
numberOfChars = Math.min(Number(numberOfChars) || 1, 10);
return Math.random().toString(36).slice(-numberOfChars);
}
@belchior
Copy link
Author

belchior commented Oct 27, 2017

The chars used to compose the hash are: abcdefghijklmnopqrstwyz0123456789

Usage:

randomHash();
> "d"

randomHash(5);
> "n2c3o"

randomHash(345);
> "vp507jn8io"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment