生成以下字符串格式-随机顺序



眼前的问题

目前,我正在努力制作一个代码生成器,原因与问题无关。

代码遵循00|letter|8string mix|letter的格式

预期最终结果的例子如下:

00b06c1161bc00aee797645b

00c435ab439e00da494a229a

中间8段字符串的快速分解导致最多需要两个字母字符和6个可以按随机顺序排列的数字。

虽然我在这方面遇到了困难,但还有一个额外的问题,那就是接受的信件有限。这些是字母abcdef

我已经为生成器列出了一个列表(acceptedChars=["a","b","c","d","e","f"](,但如何允许它根据我不确定如何实现的要求进行生成

任何关于这方面的信息都将是美妙的,如果你有任何问题,评论,我一定会回应它。

这是使用随机函数的代码的完整实现。

此代码将生成100个随机的12个字符代码。

以下代码还针对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)

随机码的样本输出(生成10(:

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

我认为random.choice就是您想要的:

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

检查整个代码是否存在问题。也许你发现了一些有用的东西。我用比O(n2(更低的复杂度做了它

它是用于验证的随机字符串生成程序 此代码还满足最大2个alpha要求

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())

希望能有所帮助。

输出看起来像:

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)}")

最新更新