Python编程:random随机模块

举报
彭世瑜 发表于 2021/08/13 23:50:32 2021/08/13
【摘要】 内置函数 import random # 随机小数[0, 1) print(random.random()) # 0.8121215001773937 # 随机小数[a, b),指定区间 print(random.uniform(1,5)) # 3.2253060854754354 # 数据整数[a, b] print(random.randint(1,5)...

内置函数

import random

# 随机小数[0, 1)
print(random.random())  # 0.8121215001773937

# 随机小数[a, b),指定区间
print(random.uniform(1,5))  # 3.2253060854754354

# 数据整数[a, b]
print(random.randint(1,5))  # 3

# 随机范围,取偶数
print(random.randrange(0,50,2))  # 14

# 随机取值string
print(random.choice("python")) # n

# 随机取值list
lst = ["one", "two", "three"]
print(random.choice(lst))  # one

# 随机取值tuple
tp = ("one", "two", "three")
print(random.choice(tp))  # two

# 取样
print(random.sample("abcdefg", 3))
# ['e', 'g', 'c']

# 洗牌
lst = [1, 2, 3, 4, 5,6]
random.shuffle(lst)
print(lst)  # [3, 2, 1, 6, 5, 4]
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

生成四位随机验证码

# 方案一:
checkcode = ""
for i in range(4): current = random.randrange(0,4) # 生成字母 if i == current: tmp = chr(random.randint(65, 90)) #  A - Z # 生成数字 else: tmp = str(random.randint(0,9)) checkcode += tmp

print(checkcode)  # 715C

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
# 方案二:
import string

codes = string.ascii_letters + string.digits
# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789

sample = random.sample(codes, 4)  # ['c', 'l', 'p', '5']

check_code = "".join(sample)

print(check_code)  # clp5
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

help(random)

FUNCTIONS choice(seq) method of Random instance Choose a random element from a non-empty sequence. randint(a, b) method of Random instance Return random integer in range [a, b], including both end points. random(...) method of Random instance random() -> x in the interval [0, 1). randrange(start, stop=None, step=1, _int=<class 'int'>) method of Random instance Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want. sample(population, k) method of Random instance Chooses k unique random elements from a population sequence or set. Returns a new list containing elements from the population while leaving the original population unchanged.  The resulting list is in selection order so that all sub-slices will also be valid random samples.  This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices). Members of the population need not be hashable or unique.  If the population contains repeats, then each occurrence is a possible selection in the sample. To choose a sample in a range of integers, use range as an argument. This is especially fast and space efficient for sampling from a large population:   sample(range(10000000), 60) seed(a=None, version=2) method of Random instance Initialize internal state from hashable object. None or no argument seeds from current time or from an operating system specific randomness source if available. For version 2 (the default), all of the bits are used if *a* is a str, bytes, or bytearray.  For version 1, the hash() of *a* is used instead. If *a* is an int, all bits are used. shuffle(x, random=None) method of Random instance Shuffle list x in place, and return None. Optional argument random is a 0-argument function returning a random float in [0.0, 1.0); if it is the default None, the standard random.random will be used. uniform(a, b) method of Random instance Get a random number in the range [a, b) or [a, b] depending on rounding.
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。

原文链接:pengshiyu.blog.csdn.net/article/details/78996588

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。