【问题标题】:Python random string/text/code generator with fixed length and some characters具有固定长度和一些字符的 Python 随机字符串/文本/代码生成器
【发布时间】:2020-01-10 21:54:36
【问题描述】:

所以,几年前,我在麦当劳的礼品卡系统中发现了一个漏洞。基本点是通过组合大约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))

【问题讨论】:

  • 目前有代码吗?
  • 我在 SO 上找到了一些,tbh。
  • 我现在添加的那个来自SO
  • 请展示一些预期输入/输出的例子
  • 好的@AlexW,等会儿

标签: python python-3.x random generate


【解决方案1】:

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

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)

【讨论】:

  • 顺便说一句,不要忘记字母周围的“”!
【解决方案2】:

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

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)

【讨论】:

  • 我做到了。请再次检查我发布的代码
【解决方案3】:

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

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)

【讨论】:

    猜你喜欢
    • 2015-09-13
    • 1970-01-01
    • 2021-03-15
    • 2012-02-13
    • 2016-04-01
    • 1970-01-01
    • 2019-12-22
    • 2018-01-25
    • 2015-02-14
    相关资源
    最近更新 更多