1. 程式人生 > >python 生成字母數字隨機碼

python 生成字母數字隨機碼

#! /usr/bin/env python
# -*- coding:utf-8 -*-


import random, string


def GenPassword(length):
numOfNum = random.randint(1, length-1)
numOfLetter = length - numOfNum
slcNum = [random.choice(string.digits)for i in range(numOfNum)]
slcLetter = [random.choice(string.ascii_letters) for i in range(numOfLetter)]
slcChar = slcNum + slcLetter
random.shuffle(slcChar)
genPwd = "".join([i for i in slcChar])

return genPwd