-1

I need to generate a number of codes with some random ints but without repeating the same code

I want this source to generate the number of codes that I input but without being the same code, generating them like a list currently, when I run the code, it generates one code like this 072546Z73810734AE but I want it to generate the number I input for example 3 072546Z73810734AE 072545Z63705472AE 072545Z72904593AE

import random
prefix = "07254"
fiveorsix = random.randint(5,6)
letterz = "Z"
sixorseven = random.randint(6,7)
threetwoorone = random.randint(1,3)
sixdigitnumber = random.randint(100000,999999)
lettersae = "AE"
code = 
prefix+str(fiveorsix)+letterz+str(sixorseven)+str(threetwoorone)+
str(sixdigitnumber)+lettersae
print(code)
  • 1
    Possible duplicate of [Generate 'n' unique random numbers within a range](https://stackoverflow.com/questions/22842289/generate-n-unique-random-numbers-within-a-range) – Poojan Aug 12 '19 at 20:37
  • 1
    Possible duplicate of [How do I create a list of random numbers without duplicates?](https://stackoverflow.com/questions/9755538/how-do-i-create-a-list-of-random-numbers-without-duplicates) – ndclt Aug 12 '19 at 20:38

1 Answers1

0

Loop through your code:

for i in range(3):
    prefix = "07254"
    fiveorsix = random.randint(5,6)
    letterz = "Z"
    sixorseven = random.randint(6,7)
    threetwoorone = random.randint(1,3)
    sixdigitnumber = random.randint(100000,999999)
    lettersae = "AE"
    code = prefix+str(fiveorsix)+letterz+str(sixorseven)+str(threetwoorone)+str(sixdigitnumber)+lettersae

    print(code)

If you want to be able to define a number, do the same inside a function:

def create_codes(n):

    for i in range(n):
        prefix = "07254"
        fiveorsix = random.randint(5,6)
        letterz = "Z"
        sixorseven = random.randint(6,7)
        threetwoorone = random.randint(1,3)
        sixdigitnumber = random.randint(100000,999999)
        lettersae = "AE"
        code = prefix+str(fiveorsix)+letterz+str(sixorseven)+str(threetwoorone)+str(sixdigitnumber)+lettersae

        print(code)

Output of create_codes(3):

072545Z71278632AE
072545Z62372104AE
072546Z72750938AE
Juan C
  • 5,846
  • 2
  • 17
  • 51