Last active
September 5, 2019 05:57
-
-
Save thejoshwolfe/4651921 to your computer and use it in GitHub Desktop.
Generates a 128-bit random number as a string.
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
/** 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