python 随机字符串和数据数字

import random
import time

# 生成随机整数,注意:比区间 包含10
num = random.randint(0,10)
#生成 0-1之间的小数点后16位的小数 随机浮点数:
num1 = random.random()
#获取0-100之间的随机偶数
num2 = random.randrange(0,101,2)
#获取0-100之间的随机浮点数
num3 = random.uniform(1,100)
#获取给定字符串中的随机字符
num4 = random.choice("abc#")
#获取给定字符串中的指定数量的随机字符
num5 = random.sample("abc#!%$",4)
#获取随机字符串
num6 = random.choice(["牛B","牛X","rubbish"])

nowTime = int(time.time())
randomNum  = nowTime % 26
#随机字母大写,小写相应转换
randomStr = chr(ord("A") + randomNum)

print("随机整数 %d,"
      "随机浮点数 %f,"
      "随机偶数 %d,"
      "随机浮点数 %f,"
      "随机字符 %s,"
      "指定数量的随机字符 %s,随机字符串 %s,随机字母大写 %s"%(num,num1,num2,num3,num4,str(num5),num6,randomStr))

运行结果:
随机整数 4,随机浮点数 0.212762,随机偶数 60,随机浮点数 14.390888,随机字符 a,指定数量的随机字符 ['%', 'a', '#', 'c'],随机字符串 牛B,随机字母大写 C

 

posted @ 2018-02-17 23:52  马鞍山  阅读(420)  评论(0编辑  收藏  举报