Skip to content

Instantly share code, notes, and snippets.

@SamuelMarks
Created March 6, 2013 01:05
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 SamuelMarks/5095883 to your computer and use it in GitHub Desktop.
Save SamuelMarks/5095883 to your computer and use it in GitHub Desktop.
uuid4()
def uuid4():
"""Generate a random UUID."""
# When the system provides a version-4 UUID generator, use it.
if _uuid_generate_random:
_buffer = ctypes.create_string_buffer(16)
_uuid_generate_random(_buffer)
return UUID(bytes=_buffer.raw)
# Otherwise, get randomness from urandom or the 'random' module.
try:
import os
return UUID(bytes=os.urandom(16), version=4)
except:
import random
bytes = [chr(random.randrange(256)) for i in range(16)]
return UUID(bytes=bytes, version=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment