1

I want to give my user a unique ID that is ten digits long and randomized. I was previously using:

user.guid = str(uuid.uuid4()).replace('-','')
'5d2f251a32ed437689e7d66575aee09f'

However, I also want to make it a bit easier to 'type in', as there some places where that is required. I was thinking about doing:

>>> str(uuid.uuid4()).replace('-','')[:10].upper()
'AA6560AB32'

What are the chances that there is a collision on this? And is there a better way to guarantee uniqueness (without storing previously added IDs)?

David542
  • 104,438
  • 178
  • 489
  • 842

1 Answers1

0

The letters abcdef in a UUID string are hex digits. So you can change them to uppercase without problems.

A UUID is a guaranteed-unique 128-bit number. If you truncate it to 40 bits (ten hex digits) it is no longer guaranteed unique. If it's vital to tell your users apart, you probably should collision-test these 40-bit numbers after generating them before assigning them to users.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • 3
    This is just wrong, an UUID is NOT guaranteed unique, it is practically unique i.e. the sun will burn out before you generate the same UUID again, but it definitely can happen. – ranieri Nov 13 '16 at 04:27