Python Forum

Full Version: Unique Random ID on Table
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,
I'm new using Python and need help from you. I use pycharm to write code.
I need to generate an unique Random ID for each transaction i create using the following table:

My table contains:
Date
Name
Address
Code --> It will be alphanumeric (between 5 to 7 length with may be special characters)
Transaction type..

Could you please assist to make it simple?

Thanks

T
What have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.

I will say that this is easy to do with a for loop and random.choice.
from random import choice
from string import ascii_letters, punctuation, digits

def get_code():
    return ''.join(choice(char_set) for _ in range(code_length))


code_length = 5
char_set = ascii_letters + punctuation + digits

possible_codes = len(char_set) ** code_length
print('Number of possible codes =', possible_codes)

print()
print('Sample:\n')
for i in range(15):
    print(get_code())
Output:
Number of possible codes = 7339040224 Sample: K~HOj By8O! 19J(\ z#?:y a2g?t r,>Li ?NK=O (-I1J g!DFf R8\O_ >D^93 1}\&t l](>_ uEIXK 2`+(~