简体   繁体   中英

Generate format following strings - random order

The Problem at hand

Currently, I am working towards making a code generator for reasons unnessesary to the question.

The Codes follow the format of 00|letter|8string mix|letter

Examples of expected end results are such:

00b06c1161bc 00aee797645b

00c435ab439e 00da494a229a

A quick breakdown of the middle 8 section string results in the requirement of a maximum of two alpha-characters and 6 numbers that can be in random order.

While Im having difficulty with this, there becomes the added problem of the limited letters accepted. These are the letters, a , b , c , d , e , and f

I've made a list ( acceptedChars=["a","b","c","d","e","f"] ) for the generator to pull from, however how to allow it to use this towards generation following the requirements I am unsure of how to pull off.

Any info on this would be wonderful, if you have any questions, comment and I will be sure to respond to it.

I think random.choice is what you want:

import random
acceptedChars = ["a","b","c","d","e","f"]
x = random.choice(acceptedChars)
y = random.choice(acceptedChars)

Here'a full implementation of the code using random function.

This code will generate you 100 random 12 char codes.

The below code also addresses the requirement of a maximum of two alpha-characters and 6 numbers that can be in random order

import random

acceptedChars = list('abcdef')
acceptedDigit = list('0123456789')

for i in range(100):
    secretCode = '00' + random.choice(acceptedChars)
    charCount = digitCount = 0

    pos1 = random.randint(1,8)
    pos2 = pos1
    while pos2 == pos1: pos2 = random.randint(1,8)

    for i in range(1,9):
        if i in (pos1,pos2):
            secretCode += random.choice(acceptedChars)
        else:
            secretCode += random.choice(acceptedDigit)

    secretCode += random.choice(acceptedChars)

    print (secretCode)

Sample output of the random codes (generated 10):

00e89642be3c
00ba75d2130e
00b56c9b906b
00da9294e87c
00b3664ce97f
00c4b6681a3e
00e6699f75cf
00d369d07a0a
00ce653a228f
00d5665f95bd

Check this whole code for your problem. Maybe you find something useful. I made it in less complexity than O(n2).

It is the program of random string generation for verification. This code also fulfills the maximum 2 alpha requirements.

import random
def code():
    acceptedChars=["a","b","c","d","e","f"]
    first = "00"
    second = random.choice(acceptedChars)
    third = ""
    fourth = random.choice(acceptedChars) 

    # for third part

    slot = random.randint(0,2)
    if (slot == 2):
        number = str(random.randint(100000,1000000))
        alpha1 = random.choice(acceptedChars)
        alpha2 = random.choice(acceptedChars)
        part1 = random.randint(0,6)
        part2 = random.randint(part1,6)
        third = number[:part1] + alpha1 + number[part1:part2] + alpha2 + number[part2:]
    elif (slot == 1):
        number = str(random.randint(1000000,10000000))
        alpha = random.choice(acceptedChars)
        slot = random.randint(0,8)
        third = number[:slot] + alpha + number[slot:]
    else:
        third = str(random.randint(10000000,100000000))

    
    return first + second + third + fourth


print(code())

Hope it helps.

Output looks like:

00d65262056f
00a317c8015e
00a334564ecf
00e14a657d9c
import string
import random

allowed_chars = string.ascii_letters[:6]

expression = ''.join(random.choices(allowed_chars + string.digits, k=8))
print(f"The generator is 00{str(expression)}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM