-1

I want to generate 10 digit unique-id in python. I have tried below methods but no-one worked

  • get_random_string(10) -> It generate random string which has probability of collision
  • str(uuid.uuid4())[:10] -> Since I am taking a prefix only, it also has probability of collision
  • sequential -> I can generate unique sequential ID by prepending 0s but it will be sequential and easier to guess. So I want to avoid sequential ids

Do we have any proper system to generate 10 digit unique-id?

SHIVAM JINDAL
  • 2,844
  • 1
  • 17
  • 34
  • checking already exists will be a fallback method which I want to avoid – SHIVAM JINDAL Feb 19 '23 at 08:47
  • 2
    How can you possibly expect to create something that's [globally] unique when you constrain it? Also, how do you know if something's unique? It depends on the context/environment – DarkKnight Feb 19 '23 at 09:01

2 Answers2

1

Depends on how many of those you need, how fast you are creating them, if they have to be unique only on your machine or for multiple machines, what characters (digits only, alphanumeric, ...), you want to allow, ... Generating unique IDs is not an easy task.

A few ideas:

  • The easiest (but of course not random at all) way is starting at 0000000000 and going up all the way up to zzzzzzzzzz by just incrementing.

  • The safest way (but probably the slowest) is storing the generated IDs, and when you generate a new one, check if it already exists ...

  • Base your ID on the current timestamp + some internal incrementor (for ids generated at the same timestamp, depending on the resolution of your timestamp) + optionally machine configuation if you need it to be unique among multiple machines + some random + some shuffling.

But the more randomness you put into your ID, the less control you have on the output, thus you need to check for collisions. Especially if the number of generated IDs is large, with respect to the number of possible IDs.

So in the end of the day, there are only two ways to guarantee uniqueness (within a certain scope)

  1. check if the newly created id was already issued before
  2. calculated by a collision-free function, and you are in control that the input to this function cannot be used twice (for instance by incrementing the input after each id you generate)
derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • The safest way is iterative one due to check for existing ids and 3rd one is too complex. Can we simplify it for low scale system? – SHIVAM JINDAL Feb 20 '23 at 06:11
-1

Try to use module so:

import hashlib
import random

def generate_id():
    # Generate a random number
    random_num = str(random.randint(0, 99999999)).encode()

    # Generate a SHA-256 hash of the random number.
    hash_obj = hashlib.sha256(random_num)
    hex_digit = hash_obj.hexdigest()

    return hex_digit[:10]
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
  • 2
    If you only take a portion of a hash, you cannot guarantee uniqueness. Furthermore, if it's just based on the randomnumber, after 99999999 id's you have a guaranteed collision ... – derpirscher Feb 19 '23 at 08:47
  • 1
    @derpirscher Yes sir you're right, it was just an example. – Sunderam Dubey Feb 19 '23 at 08:51