具有固定长度和一些字符的Python随机字符串/文本/代码生成器

2024-10-01 07:47:40 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,几年前,我在麦当劳的礼品卡系统中发现了一个漏洞。基本点是通过组合大约15-20张卡片和它们的代码,我得到了一个点,第3和第7个字符是相同的,而其他字符是完全随机的

我可能在我所在地区(土耳其)举行的新可口可乐节上也发现了同样的事情。我只是想问,我如何开发一个带有随机模块的代码,它将通过检查特定的算法来随机创建代码,例如,让我们再次使用相同的第3和第7个字符

通过保留它们,随机生成8个编号/字母代码。我还认为使用while-True会更好地生成无限量的它们。最后,我将把它们添加到一个列表中。但是,我需要中间部分的帮助。

import random
import string

def randomStringDigits(stringLength=6):
    lettersAndDigits = string.ascii_letters + string.digits
    return ''.join(random.choice(lettersAndDigits) for i in range(stringLength))

print ("Generating a Random String including letters and digits")
while True:
    print ("First Random String is  ", randomStringDigits(8))

Tags: 代码importtruestringrandomprintdigitswhile
3条回答

你快到了。我只是稍微调整了一下-您可以按原样进行调整:

import random

numcount = 5
fstring = ""
length_of_code_you_want = 12
position_to_keep_constant_1 = 2 # first position to keep constant is position 3, index 2
position_to_keep_constant_2 = 6 # 2nd position to keep constant is position 7, index 6
constant_1 = "J" # the value of the constant at position 3
constant_2 = "L" # the value of the constant at position 7

for num in range(length_of_code_you_want): #strings are 19 characters long
    if random.randint(0, 1) == 1:
        x = random.randint(1, 8)
        x += 96
        fstring += (chr(x).upper())
    elif not numcount == 0:
        x = random.randint(0, 9)
        fstring += str(x)
        numcount -= 1

list_out = list(fstring)
list_out[position_to_keep_constant_1] = str(constant_1)
list_out[position_to_keep_constant_2] = str(constant_2)
string_out = "".join(list_out)
print(string_out)

我不确定这是否合法,但问题很简单

import random
import string

value_of_seven = 7
value_of_three = 3

def randomString(stringLength=10):
    """Generate a random string of fixed length """
    letters = string.ascii_lowercase
    _string = ''.join(random.choice(letters) for i in range(stringLength))
    print ("Random String is ", randomString() )
    return _string

x = 0
string_set = set()
while x <= 10:
    x += 1
    rand_str = randomString()
    if rand_str[-1, 3] is value_of_seven and rand_str[1, 3] is value_of_three:
        string_set.add(rand_str)

但我们真的需要知道,只是小写字母?大写

另外,如果您试图在这些位置使用相同的内容生成它们,您仍然会在同一点进行切片,并在末尾添加字符串

好的,这是符合您要求的工作版本

import random
import string

value_of_seven = '7'
value_of_three = '3'


def _random(stringLength=5):
    """Generate a  string of Ch/digits """
    lett_dig = string.ascii_letters + string.digits
    return ''.join(random.choice(lett_dig) for i in range(stringLength))


if __name__ == '__main__':
    s = _random()
    s = s[:2] + value_of_three + s[2:]
    s = s[:6] + value_of_seven + s[6:]
    print(s)

我不确定这样做是否真的很优雅,但我只是将随机字母保留在一个列表中,在它们各自的位置插入值,然后将列表元素连接到一个字符串中

import random
import string
first = '3' # the first value to place in the 3rd position
second = '7' # the second value to place in the 7th position of the string

def random_string_digits(string_length):
    values = string.ascii_lowercase+string.digits
    output = [random.choice(values) for i in range(string_length)] 
    # the final string length will be equal to string_length+2
    # now that we created the random list of characters, it's time to insert the letter in it
    output.insert(2, first) # insert the first value in the 3rd position
    output.insert(6, second) # insert the second value in the 7th position
    return ''.join(output)

相关问题 更多 >