python random模块

 
import random
 
print(random.random())
print(random.randint(1, 2))
print(random.randrange(1, 10))
 
#验证码例子
def id_code():
    import random
    date = ""
    for p in range(4):
        i = random.randrange(0, 4)
        if i == 0 or i == 3:
            k = random.randrange(0, 10)
            date = date + str(k)
        else:
            j = random.randrange(65, 91)
            date = date + chr(j)
    return date
if __name__ == '__main__':
    print(id_code())

 

posted @ 2017-03-24 00:06  君何在  阅读(144)  评论(0编辑  收藏  举报