Created
March 6, 2013 01:05
-
-
Save SamuelMarks/5095883 to your computer and use it in GitHub Desktop.
uuid4()
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
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