Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unique Random ID on Table
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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`+(~
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample random, unique string pairs from a list without repetitions walterwhite 1 448 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  generating random string unique forever Skaperen 5 2,324 Aug-20-2021, 07:15 AM
Last Post: Gribouillis
  Generate unique random numbers from different lists Takeshio 5 3,764 May-24-2019, 07:29 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020