Python标准库--random
[1] 转载自 https://www.cnblogs.com/duking1991/p/6121300.html
从指定范围内,按指定基数递增的集合中 ,这篇文章就是对python生成随机数的应用程序的部分介绍。
随机整数:
>>> import random
>>> random.randint(0,99)
21
随机选取0到100间的偶数:
>>> import random
>>> random.randrange(0, 101, 2)
42
随机浮点数:
>>> import random
>>> random.random()
0.85415370477785668
>>> random.uniform(1, 10)
5.4221167969800881
随机字符:
>>> import random
>>> random.choice('abcdefg&#%^*f')
'd'
多个字符中选取特定数量的字符:
>>> import random
random.sample('abcdefghij',3)
['a', 'd', 'b']
多个字符中选取特定数量的字符组成新字符串:
>>> import random
>>> import string
>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'
随机选取字符串:
>>> import random
>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
'lemon'
洗牌:
>>> import random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle(items)
>>> items
[3, 2, 5, 6, 4, 1]
[2] 转载自https://blog.csdn.net/zheng_lan_fang/article/details/76684761
以下是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)
下面是生成指定长度字母数字随机序列的代码: