python2之随机数

python random使用方法

介绍关于python生成随机数与random模块

##生成随机数在指定范围内
#coding=utf-8
import random
print random.uniform(10,20) #生成10-20
print random.uniform(20,10) #生成10-20的随机浮点数

print random.randint(10,20) #生成10-20的随机整数
print random.randint(20,20) #生成20
print random.randint(20,10) #错误

#生成0-100间的一个偶数
print random.randrange()

#生成随机字符
print random.choice("abcdefijklmnopqlstuvwxyz")

#多个字符选中特定数量的字符组成新的字符串
print sample("abcdefijklmnopqlstuvwxyz", 3)
print type(random.sample("abcdefijklmnopqlstuvwxyz", 3)) #list

#多个字符选定新的字符组成字符串
from string
print type(string.join(random.sample("abcdefijklmnopqlstuvwxyz", 3))) #str
print string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).replace(" ","")
#不加replace 输出结果为 j h f
#加上replace 输出结果为 jhf


#洗牌
item = [1, 2, 3, 4, 5, 6]
random.shuffle(item)
print item
##输出
#[3, 4, 5, 1, 2, 6]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值