random
以下是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用法
下面是生成一个包含大写字母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)
下面是生成指定长度字母数字随机序列的代码: