Skip to content

Instantly share code, notes, and snippets.

@bytespider
Forked from 140bytes/LICENSE.txt
Created August 31, 2011 08:18
Show Gist options
  • Save bytespider/1183065 to your computer and use it in GitHub Desktop.
Save bytespider/1183065 to your computer and use it in GitHub Desktop.
UUID generator
function makeUUID(){
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(
a, // character
b // placeholder for random number
){
return
b = Math.random() * 16,
(
a == "y"
?
b & 3 | 8 // bring the number down to between 9 - 12
:
b | 0 // |0 insures and integer
).toString(16)
})
}
function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(a,b){return b=Math.random()*16,(a=="y"?b&3|8:b|0).toString(16)})}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "makeUUID",
"description": "Makes a universally unique identifier, Version 4",
"keywords": [
"uuid",
"generator"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var makeUUID = function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(a,b){return b=Math.random()*16,(a=="y"?b&3|8:b|0).toString(16)})};
document.getElementById( "ret" ).innerHTML = makeUUID()
</script>
@nikola
Copy link

nikola commented Aug 31, 2011

There's already a shorter version: https://gist.github.com/982883

@bytespider
Copy link
Author

Thank you, Jed also made me aware of this.

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