简体   繁体   English

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

[英]Python random string/text/code generator with fixed length and some characters

So, a few years ago I have found an exploit in the giftcard system at McDonalds.所以,几年前,我在麦当劳的礼品卡系统中发现了一个漏洞。 The basic point is by combining about 15-20 cards, and their codes, I got a point that 3rd and the 7th characters are the same while the others are completely random.基本点是通过组合大约 15-20 张卡片和它们的代码,我得到了一个点,即第 3 个和第 7 个字符是相同的,而其他字符是完全随机的。

I might have found the same thing in the new Coke festival that is taken in my area (Turkey).我可能在我所在地区(土耳其)举办的新可乐节中发现了同样的事情。 I just wanted to ask, how can I develop a code with random module that it will randomly create codes by going over a specific algorithm, for example let's go with the same 3rd and 7th characters again.我只是想问,我怎样才能开发一个带有随机模块的代码,它会通过遍历特定算法来随机创建代码,例如让我们再次使用相同的第 3 个和第 7 个字符。

By keeping them, randomly generate 8 numbered/lettered codes.通过保留它们,随机生成 8 个编号/字母代码。 Also I think using the while True would be better to generate like infinite amount of them.另外我认为使用 while True 会更好地生成无限量的它们。 I will finally add them into a list.我最终会将它们添加到列表中。 However, I need help in the middle part.但是,我需要中间部分的帮助。

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

Not sure about the legality of that, but the problem is easy enough.不确定这样做的合法性,但问题很简单。

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)

But we really need to know, just letters lower case?但我们真的需要知道,只是字母小写? Upper case?大写?

Also if your trying to generate them with the same things in those places you would still slice at the same point and add the string on the end.此外,如果您尝试在这些地方使用相同的东西生成它们,您仍然会在同一点切片并在末尾添加字符串。

Ok here is working version with your requirements好的,这是符合您要求的工作版本

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)

You're almost there.您快到了。 I've just tweaked it a little - you could adjust it as is:我只是稍微调整了一下 - 你可以按原样调整它:

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)

I'm not sure if this is really elegant to do but I'd just keep the random letter in a list, insert the values at their respective positions and then join the list elements in a string.我不确定这样做是否真的很优雅,但我只是将随机字母保留在列表中,在各自的位置插入值,然后将列表元素加入字符串中。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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