Generate a unique number for each entry

It really depends on what you need but the random package is not a good goto. Thats for random generation, not unique number generation.

import uuid
uuid.uuid4().int

this will give you a unique integer each time it is run. This number is universally unique so in theory its unique to your machine at the time of execution so the chance of collision is infinitesimally small

That being said this number is going to be VERY big. It really depends on what your uniqueness needs are. If you simply need a number which is unique per session you can have an incrementing counter

__counter = 0
def get_next_int_id():
     global __counter
     __counter +=1
     return __counter

This will give you a id that will always be unique on this run of your program.

2 Likes