使用python生成促销代码

发布于 2024-08-18 08:01:22 字数 85 浏览 7 评论 0原文

通过使用 python 语言,这将是一种聪明/有效的生成促销代码的方法。 喜欢用于生成折扣优惠券的特殊号码。 喜欢:1027828-1

谢谢

By using python language, what would be a clever / efficient way of generating promotion codes.
Like to be used for generating special numbers for discount coupons.
like: 1027828-1

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

郁金香雨 2024-08-25 08:01:22

以下内容并不是特别Pythonic或特别高效,但可能就足够了:

 import random
 def get_promo_code(num_chars):
     code_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
     code = ''
     for i in range(0, num_chars):
         slice_start = random.randint(0, len(code_chars) - 1)
         code += code_chars[slice_start: slice_start + 1]
     return code

The following isn't particularly pythonic or particularly efficient, but it might suffice:

 import random
 def get_promo_code(num_chars):
     code_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
     code = ''
     for i in range(0, num_chars):
         slice_start = random.randint(0, len(code_chars) - 1)
         code += code_chars[slice_start: slice_start + 1]
     return code
半衾梦 2024-08-25 08:01:22

1027828-1 非常小。攻击者仅使用几行代码甚至几天的时间就可以做出约百万次猜测。

这是使用 python 生成难以预测的数字的好方法,它可以在 Linux 和 Windows 下运行。它是为了二进制安全而进行 base64 编码的,具体取决于您使用它做什么,您可能需要 urllib.urlencode() 但我会避免使用 base10,因为它不会存储那么多信息。

import os
import base64

def secure_rand(len=8):
    token=os.urandom(len)
    return base64.b64encode(token)

print(secure_rand())

附带说明一下,这会生成一个完整的字节,即 base256。 256^8 是 18446744073709551616 应该足够大。

正如指出的那样,base64 对于人类来说并不是一个很好的标记。考虑使用替代编码,例如 url-safe base64 或 humanhash 因为它们更容易输入。

1027828-1 is extremely small. An attacker can make a ~million guesses using only a couple lines of code and maybe a few days.

This is a good way to produce a hard to predict number using python, it works under linux and windows. It is base64'ed for binary safety, depending what you are doing with it you might want to urllib.urlencode() but I would avoid base10 because it doesn't store as much information.

import os
import base64

def secure_rand(len=8):
    token=os.urandom(len)
    return base64.b64encode(token)

print(secure_rand())

As a side note this is generating a full byte, which is base256. 256^8 is 18446744073709551616 which should be large enough.

As it was pointed out base64 isn't a very good token for humans to use. Consider an alternate encoding like url-safe base64 or perhaps humanhash as they would be easier to type in.

沐歌 2024-08-25 08:01:22

如果您需要 6 位数字 #,您可以这样做,直到找到唯一值:

import random
print str(random.randint(100000, 999999))

或按顺序...

if you need a 6-digit # you could do this until you found a unique value:

import random
print str(random.randint(100000, 999999))

or go sequentially...

傲影 2024-08-25 08:01:22

试试这个:

import random

coupon = open("coupons.txt", "a")

def generate(amount):

    for x in range(0, amount):

        a = random.randint(1000, 9999)
        a = str(a)
        b = random.randint(1000, 9999)
        b = str(b)
        c = random.randint(1000, 9999)
        c = str(c)

        total = ""
        total = str(total)
        total = a + " " + b + " " + c

        coupon.write(total)
        coupon.write("\n")


amount = int(input("How many coupons do you want to generate: "))

generate(amount)

coupon.close()

print("\nCode's have been generated!")

只要你愿意,你就可以制作优惠券。他们还保存到名为 coupons.txt 的 txt 文件中。

Try this:

import random

coupon = open("coupons.txt", "a")

def generate(amount):

    for x in range(0, amount):

        a = random.randint(1000, 9999)
        a = str(a)
        b = random.randint(1000, 9999)
        b = str(b)
        c = random.randint(1000, 9999)
        c = str(c)

        total = ""
        total = str(total)
        total = a + " " + b + " " + c

        coupon.write(total)
        coupon.write("\n")


amount = int(input("How many coupons do you want to generate: "))

generate(amount)

coupon.close()

print("\nCode's have been generated!")

You can make the coupons as long as you want. They save into a txt file called coupons.txt also.

稀香 2024-08-25 08:01:22

我对此提出了一个我认为相当聪明的答案,但依赖于一些可能不适合您的情况的假设。

  • 生成的代码是纯数字的。
  • 从技术上来说,生成的代码是可变长度的; [10, 20]。

如果这些对您有用,那么这个解决方案也可能对您有用:

def code(seed = None):
    if (not seed) or (type(seed) != str) or (len(seed) < 10):
        seed = str(uuid.uuid4())[:10]

    code = ""
    for character in seed:
        value = str(ord(character))
        code += value

    return code[:20]

在此函数中,使用字符串类型的种子作为代码的基础。对于字符串中的每个字符,将其转换为其 ASCII 表示形式,然后将其附加到代码中。

默认情况下,该函数生成如下代码:'97534957569756524557',并且可以使用任意种子调用。例如...

code("pcperini's answer") == '11299112101114105110'
code(str(time.time())) == '49524956514950505257'

I've come up with an answer for this that I think is fairly clever, but relies on a couple of assumptions that might not be true for your situation.

  • The resulting code is purely numeric.
  • The resulting code is technically variable-length; [10, 20].

If these work for you, then so might this solution:

def code(seed = None):
    if (not seed) or (type(seed) != str) or (len(seed) < 10):
        seed = str(uuid.uuid4())[:10]

    code = ""
    for character in seed:
        value = str(ord(character))
        code += value

    return code[:20]

In this function, a string-typed seed is used as the base of the code. For each character in the string, convert it into its ASCII representation, then append it to the code.

By default, the function yields codes like this: '97534957569756524557', and can be invoked with any arbitrary seed. For example...

code("pcperini's answer") == '11299112101114105110'
code(str(time.time())) == '49524956514950505257'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文