首页 > 其他 > 详细

关于random函数

时间:2020-05-24 11:12:51      阅读:42      评论:0      收藏:0      [点我收藏+]

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

关于random函数

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!