Skip to content

Instantly share code, notes, and snippets.

@thejoshwolfe
Last active September 5, 2019 05:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thejoshwolfe/4651921 to your computer and use it in GitHub Desktop.
Save thejoshwolfe/4651921 to your computer and use it in GitHub Desktop.
Generates a 128-bit random number as a string.
/** random 128-bit number as a string */
function random128() {
var result = "";
for (var i = 0; i < 8; i++)
result += String.fromCharCode(Math.random() * 0x10000);
return result;
}
/** random 128-bit number in canonical uuid format. all bits are random. */
function random128Hex() {
function random16Hex() { return (0x10000 | Math.random() * 0x10000).toString(16).substr(1); }
return random16Hex() + random16Hex() +
"-" + random16Hex() +
"-" + random16Hex() +
"-" + random16Hex() +
"-" + random16Hex() + random16Hex() + random16Hex();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment