使用迭代方法取随机码,而不是全部返回,保存函数,为以后开发系统使用。
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from random import choice codeOrig = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890" def makePromoteCode(codeLength=4): Code = ‘‘ for i in range(0,codeLength): Code += choice(codeOrig) return Code def ReturCode(codeLength=4,codeCount=10): for i in range(0,codeCount): Code = makePromoteCode(codeLength=codeLength) yield Code #print (Code) s = ReturCode(8,4) print (s.__next__()) print (s.__next__()) print (s.__next__()) print (s.__next__())
如果是需要一次性返回随机码方法为:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import random codeOrig = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890" def makePromoteCode(codeLength=12,codeCount=200): for i in range(codeCount): promotecode = "" for x in range(codeLength): promotecode += random.choice(codeOrig) print (promotecode) #a = ‘abcdefghijklmnopqrstuvwxyz‘ if __name__ == ‘__main__‘: makePromoteCode(34,10)
时间: 2024-12-15 22:20:51