|
使用迭代方法取随机码,而不是全部返回,保存函数,为以后开发系统使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #!/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__())
|
如果是需要一次性返回随机码方法为:
1
2
3
4
5
6
7
8
9
10
11
12
13
| #!/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)
|
|
|