Skip to content

Instantly share code, notes, and snippets.

@jackrusher
Created December 20, 2012 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackrusher/4341940 to your computer and use it in GitHub Desktop.
Save jackrusher/4341940 to your computer and use it in GitHub Desktop.
generate_uuid = () ->
"aaaaaaaa-aaaa-4aaa-baaa-aaaaaaaaaaaa".replace /[ab]/g, (ch) ->
rnd = Math.random() * 16 | 0
out = (if ch is "a" then rnd else (rnd & 0x3 | 0x8))
out.toString 16
#
# Conforms to: http://tools.ietf.org/html/rfc4122
#
# 4.4. Algorithms for Creating a UUID from Truly Random or
# Pseudo-Random Numbers
#
# The version 4 UUID is meant for generating UUIDs from truly-random or
# pseudo-random numbers.
#
# The algorithm is as follows:
# o Set the two most significant bits (bits 6 and 7) of the
# clock_seq_hi_and_reserved to zero and one, respectively.
# o Set the four most significant bits (bits 12 through 15) of the
# time_hi_and_version field to the 4-bit version number from
# Section 4.1.3.
# o Set all the other bits to randomly (or pseudo-randomly) chosen
# values.
#
# typedef struct {
# unsigned32 time_low;
# unsigned16 time_mid;
# unsigned16 time_hi_and_version;
# unsigned8 clock_seq_hi_and_reserved;
# unsigned8 clock_seq_low;
# byte node[6];
# } uuid_t;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment