-4

I want to generate a unique id which will have 10 characters. These will include 32 characters/numerics I.e 'a' to 'v' and '0' to '9'.

Adjacently repeated characters are disallowed for e.g '1hdhusiit' in this 'ii' is adjacent.

Each key should be unique.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
uroosa
  • 1
  • 3
    https://docs.python.org/3.4/library/uuid.html – TigerhawkT3 Jul 07 '15 at 23:41
  • 7
    Hi and welcome to Stack Overflow. here we expect you to have a go at solving the problem yourself, and to tell us what you've researched, and show us the code you've tried (even if it isn't working), in order for us to help you. Have a go, then come back and show us what you tried. – Taryn East Jul 07 '15 at 23:46

1 Answers1

-1
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
    for i 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 your system does not provide time.time() support for under 1 second of resolution. If that is the case, this will only be able to generate one new unique id per second.

bytesized
  • 1,502
  • 13
  • 22
  • This does not prevent repeated characters. And it produces highly predictable IDs. – augurar Jul 08 '15 at 06:28
  • @augurar Edited to fix repeating character problem. And you said nothing about making them unpredictable. The closest you will likely get to an unpredictable unique key is hashing something, but as pointed out [here](http://stackoverflow.com/a/2444336/4103025), hashes aren't *guaranteed* to be unique (although it is very, very likely). But if you want truly unique keys, they should be predictable. That is what allows you to say with confidence that they are unique. – bytesized Jul 08 '15 at 17:05
  • Thankyou so much @bytesized that's so helping and nice of you, I'll surelywork it out and can I have your twitter id please?:) – uroosa Jul 09 '15 at 21:00
  • What are the system requirements for time.time() to make it work? @bytesized – uroosa Jul 09 '15 at 21:03