Skip to content

Instantly share code, notes, and snippets.

@bmc
Created February 23, 2012 16:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmc/1893440 to your computer and use it in GitHub Desktop.
Save bmc/1893440 to your computer and use it in GitHub Desktop.
Generate UUID, using Math.random(), in CoffeeScript
# RFC1422-compliant Javascript UUID function. Generates a UUID from a random
# number (which means it might not be entirely unique, though it should be
# good enough for many uses). See http://stackoverflow.com/questions/105034
uuid = ->
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) ->
r = Math.random() * 16 | 0
v = if c is 'x' then r else (r & 0x3|0x8)
v.toString(16)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment