0

I'm trying to do a script that generate a 20 digit random string. But these strings have to be unique.

Imagine that I generate today 20000 random string and tomorrow 20000 other strings I need to be sure that I have 40000 different string.

The problem is the amount of data I have nearly 700000 serial by day and don't know how to optimize it to be sure to have unique serial with competitive time ...

Thanks you for your help.

Need to be compliant to this https://ec.europa.eu/health/sites/health/files/files/eudralex/vol-1/reg_2016_161/reg_2016_161_en.pdf

I can't do incremental.

2
  • 3
    Is there any reason you can't just start with 00000000000000000001 and move on to 00000000000000000002? Commented Aug 17, 2017 at 14:48
  • Need to be random string not incremental with some complexity Commented Aug 17, 2017 at 15:26

3 Answers 3

1

I agree with @bendl's comment - if you need it to be 20 digits you could just increase by one value each time you need a new string. If it doesn't have to be 20 digits, I'd say take a look at the UUID module for python: https://docs.python.org/2/library/uuid.html

This module creates unique IDs, could be what you're looking for. Good luck!

Sign up to request clarification or add additional context in comments.

Comments

0

To ensure uniqueness you can generate string as combination of UUID and timestamp. For example

>>> import uuid
>>> import time
>>> my_string = "{}-{}".format(uuid.uuid4(), time.time())
>>> print my_string

'6cc452ac-5b30-47df-8ea4-7170ec1979b1-1502985399.0055182'

3 Comments

but there is possibility of duplicate with this method. I can't even have 1/10000000 probability it have to be 0
Having timestamp as part of the generated string will guarantee value uniqueness, because your timestamp will continuously increase as time goes and will never repeat in observable future. You could achieve same result by using SQL sequence, just example below shows how to do this in Python. Additionally, example above scales better than SQL sequences for really big data volumes, should you need this
But what about my 20 character maximum :/ and only alphanum Thanks you for your help btw
0

Essentially you need to create a new random string and verify it does not exist in your current list (if you can't check your current list, this can't be done).

Reference https://docs.python.org/3/library/random.html#random.choices

import string
import random

x = 0
itemlist = []

while x < 2000:
    newitem = ''.join(random.choice(string.ascii_uppercase + string.digits) 
    for _ in range(20))

    if newitem not in itemlist:
        itemlist.append(newitem)
        x = x + 1
        print newitem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.