random

random

  • 随机数
  • 所有的随机模块都是伪随机
import random
# random() 获取0 - 1之间的随即小数
# 格式:random.random()
# 返回值:随机0 - 1之间的小数
print(random.random())
0.03539160320473711
# 利用random函数,生成0 - 100之间的整数
# choice() 随机返回序列中的某个值
# 格式:random.choice(序列)
# 返回值:序列中的某个值

l = [str(i)+"haha" for i in range(10)]
print(l)
rst = random.choice(l)
print(rst)
['0haha', '1haha', '2haha', '3haha', '4haha', '5haha', '6haha', '7haha', '8haha', '9haha']
8haha
# shuffle()随机打乱列表
# 格式:random.shuffle(列表)
# 返回值:打乱顺序之后的列表

l1 = [i for i in range(10)]
print(l1)

random.shuffle(l1)
print(l1)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 8, 5, 3, 9, 1, 4, 6, 2, 7]
# randint(a,b): 返回一个a到b之间的随机整数,包含a和b
print(random.randint(0,100))
help(random.randint)
25
Help on method randint in module random:

randint(a, b) method of random.Random instance
    Return random integer in range [a, b], including both end points.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值