概述
【Python模块学习】3、random模块
参考:1、官网;2、别人的
以下是random模块的方法:
实例:
Basic examples:
- >>> random() # 随机浮点数: 0.0 <= x < 1.0
- 0.37444887175646646
- >>> uniform(2.5, 10.0) # 随机浮点数: 2.5 <= x < 10.0
- 3.1800146073117523
- >>> randrange(10) # 0-9的整数:
- 7
- >>> randrange(0, 101, 2) # 0-100的偶数
- 26
- >>> choice([’win’, ‘lose’, ‘draw’]) # 从序列随机选择一个元素
- ’draw’
- >>> deck = ’ace two three four’.split()
- >>> shuffle(deck) # 对序列进行洗牌,改变原序列
- >>> deck
- [’four’, ‘two’, ‘ace’, ‘three’]
- >>> sample([10, 20, 30, 40, 50], k=4) # 不改变原序列的抽取指定数目样本,并生成新序列
- [40, 10, 50, 30]
- >>> # 6次旋转红黑绿轮盘(带权重可重复的取样),不破坏原序列,weight[18,18,2]
- >>> choices([’red’, ‘black’, ‘green’], [18, 18, 2], k=6)
- [’red’, ‘green’, ‘black’, ‘black’, ‘red’, ‘black’]
- >>> # 德州扑克计算概率Deal 20 cards without replacement from a deck of 52 playing cards
- >>> # and determine the proportion of cards with a ten-value
- >>> # (a ten, jack, queen, or king).
- >>> deck = collections.Counter(tens=16, low_cards=36)
- >>> seen = sample(list(deck.elements()), k=20)
- >>> seen.count(’tens’) / 20
- 0.15
- >>> # 模拟概率Estimate the probability of getting 5 or more heads from 7 spins
- >>> # of a biased coin that settles on heads 60% of the time.’H’的概率是0.6,“T”的概率是1-0.6
- >>> trial = lambda: choices(‘HT’, cum_weights=(0.60, 1.00), k=7).count(‘H’) >= 5
- >>> sum(trial() for i in range(10000)) / 10000
- 0.4169
- >>> # Probability of the median of 5 samples being in middle two quartiles
- >>> trial = lambda : 2500 <= sorted(choices(range(10000), k=5))[2] < 7500
- >>> sum(trial() for i in range(10000)) / 10000
- 0.7958
- >>> from statistics import mean
- >>> from random import choices
- >>> data = 1, 2, 4, 4, 10
- >>> means = sorted(mean(choices(data, k=5)) for i in range(20)) # mean是求平均
- >>> print(f‘The sample mean of {mean(data):.1f} has a 90% confidence ’
- f’interval from {means[1]:.1f} to {means[-2]:.1f}’) # 这里的f用法
>>> random()
# 随机浮点数:
0.0 <= x < 1.0
0.37444887175646646
>>> uniform(2.5, 10.0)
# 随机浮点数:
2.5 <= x < 10.0
3.1800146073117523
>>> randrange(10)
# 0-9的整数:
7
>>> randrange(0, 101, 2)
# 0-100的偶数
26
>>> choice(['win', 'lose', 'draw'])
# 从序列随机选择一个元素
'draw'
>>> deck = 'ace two three four'.split()
>>> shuffle(deck)
# 对序列进行洗牌,改变原序列
>>> deck
['four', 'two', 'ace', 'three']
>>> sample([10, 20, 30, 40, 50], k=4)
# 不改变原序列的抽取指定数目样本,并生成新序列
[40, 10, 50, 30]
>>> # 6次旋转红黑绿轮盘(带权重可重复的取样),不破坏原序列,weight[18,18,2]
>>> choices(['red', 'black', 'green'], [18, 18, 2], k=6)
['red', 'green', 'black', 'black', 'red', 'black']
>>> # 德州扑克计算概率Deal 20 cards without replacement from a deck of 52 playing cards
>>> # and determine the proportion of cards with a ten-value
>>> # (a ten, jack, queen, or king).
>>> deck = collections.Counter(tens=16, low_cards=36)
>>> seen = sample(list(deck.elements()), k=20)
>>> seen.count('tens') / 20
0.15
>>> # 模拟概率Estimate the probability of getting 5 or more heads from 7 spins
>>> # of a biased coin that settles on heads 60% of the time.'H'的概率是0.6,“T”的概率是1-0.6
>>> trial = lambda: choices('HT', cum_weights=(0.60, 1.00), k=7).count('H') >= 5
>>> sum(trial() for i in range(10000)) / 10000
0.4169
>>> # Probability of the median of 5 samples being in middle two quartiles
>>> trial = lambda : 2500 <= sorted(choices(range(10000), k=5))[2]
< 7500
>>> sum(trial() for i in range(10000)) / 10000
0.7958
>>> from statistics import mean
>>> from random import choices
>>> data = 1, 2, 4, 4, 10
>>> means = sorted(mean(choices(data, k=5)) for i in range(20))
# mean是求平均
>>> print(f'The sample mean of {mean(data):.1f} has a 90% confidence '
f'interval from {means[1]:.1f} to {means[-2]:.1f}')
# 这里的f用法
下面是生成一个包含大写字母A-Z和数字0-9的随机4位验证码的程序
- import random
- checkcode = ”
- for i in range(4):
- current = random.randrange(0,4)
- if current != i:
- temp = chr(random.randint(65,90))
- else:
- temp = random.randint(0,9)
- checkcode += str(temp)
- print(checkcode)
import random
checkcode = ''
for i in range(4):
current = random.randrange(0,4)
if current != i:
temp = chr(random.randint(65,90))
else:
temp = random.randint(0,9)
checkcode += str(temp)
print(checkcode)
下面是生成指定长度字母数字随机序列的代码:
最后
以上就是过时冬日为你收集整理的Python的random模块学习的全部内容,希望文章能够帮你解决Python的random模块学习所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复