python unique id generation -
i want generate unique id have 10 characters. these include 32 characters/numerics i.e 'a' 'v' , '0' '9'.
adjacently repeated characters disallowed e.g '1hdhusiit' in 'ii' adjacent.
each key should unique.
def unique_id(): available_chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', \ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', \ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; available_char_count = len(available_chars) timestamp = int(time.time() * 1000) # first char char_index = timestamp % available_char_count timestamp /= available_char_count uuid = available_chars[char_index] last_char = available_chars[char_index] available_char_count -= 1 in xrange(9): currently_available_chars = list(available_chars) currently_available_chars.remove(last_char) char_index = timestamp % available_char_count timestamp /= available_char_count uuid = currently_available_chars[char_index] + uuid last_char = currently_available_chars[char_index] return uuid
this should work unless system not provide time.time()
support under 1 second of resolution. if case, able generate 1 new unique id per second.
Comments
Post a Comment