关于random函数

import random

print random.random()          #用于生成一个0到1的随机符点数: 0 <= n < 1.0
print random.randint(1,8)      #用于生成1到8的随机数,可以生成右边界的数。
print random.randrange(1,10,2) #2是步长,用于生成1、3、5、7、9的随机数,不可以生成右边界数。
 
#一个生成随机码的小程序

import random

checkcode = ''

for in range(4):

    current = random.randrange(0,4)

    if current != i:

        temp = chr(random.randint(65,90))

    #ord()函数主要用来返回对应字符的ascii码,chr()主要用来表示数字对应的ascii码字符。两个函数互为反操作。

    else:

        temp = random.randint(0,9)

    checkcode += str(temp)          #str将数字型数字变字符型数字

print checkcode

原文地址:https://www.cnblogs.com/phoenix-mountain/p/12945890.html