Python模块学习:random模块

2022-07-3021:42:16编程语言入门到精通Comments907 views字数 1884阅读模式

曾多次提到过random模块,在这节将详细的介绍一下random模块的用法。random模块主要为我们提供一些生成随机数的功能,下面表格总结了random模块中几个常用函数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

函数名功能
random.random()生成一个[0,1)的实数。
random.randint(a,b)生成一个[a,b]的数字,包含a和b。
random.randrange(a,b)生成一个[a,b)的数字,包含a,不包含b。
random.shuffle(x)把序列x打乱。
random.choice(x)从x中返回一个随机项,x需要是一个可变序列。
random.uniform(a,b)生成一个a,b之间的浮点数,区间可以是浮点数。

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

 1. random.random()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

生成一个0-1的随机浮点数,看下面的例子:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

1
2
3
4
5
6
7
import random
= random.random()
= random.random()
= random.random()
print(a)
print(b)
print(c)

输出结果为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

1
2
3
0.787965560268062
0.20524486179856316
0.8732074424182436

我们可以看出返回的浮点数的值和位数都不一定相同。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

2. random.randint(a,b)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

我需要注意randint和randrange的区别,randint是包含a和b,后面会提到的randrange是不含a和b的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

我们可以通过测试来看一下是否包含上下限,代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

1
2
3
4
5
6
7
import random
= random.randint(1,3)
= random.randint(1,3)
= random.randint(1,3)
print(a)
print(b)
print(c)

输出结果为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

1
2
3
3
1
2

我们可以看出是包含a和b的值的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

3. random.randrange(a,b)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

我们主要通过测试来看一下这种方法是否包含a和b的值,看下面代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

1
2
3
4
5
6
7
8
9
import random
= random.randrange(1,3)
= random.randrange(1,3)
= random.randrange(1,3)
= random.randrange(1,3)
print(a)
print(b)
print(c)
print(d)

输出结果为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

1
2
3
4
2
2
1
2

通过测试结果我们可以看出只有1和2,是不包含下界3的,大家也可以自己尝试一下运行一下。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

4. random.shuffle(x)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

random.shuffle(x)函数我们在第二节中使用过,它可以将一个序列进行重新排序,但是需要注意,它只针对于可改变的序列,因此它常被用于打乱列表中的元素。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

1
2
3
4
5
import random
= ['d','o','t','c','p','p']
print('打乱前的列表:',a)
random.shuffle(a)
print('打乱后的列表:',a)

输出结果为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

1
2
打乱前的列表: ['d''o''t''c''p''p']
打乱后的列表: ['c''d''p''p''t''o']

 5. random.choice(x)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

random.choice(x)可以返回一个序列中的一个随机元素,它的使用方式如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

1
2
3
4
5
6
7
import random
= ['d','o','t','c','p','p']
= 'dotcpp'
= ('d','o','t','c','p','p')
print(random.choice(a))
print(random.choice(b))
print(random.choice(c))

输出结果为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

1
2
3
o
d
p

6. random.uniform(a,b)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

用于生成指定范围内的随机浮点数,代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

1
2
3
4
5
6
7
import random
= random.uniform(1.1,3.3)
= random.uniform(5,6)
= random.uniform(100,200)
print(a)
print(b)
print(c)

输出结果为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

1
2
3
2.5802656579509087
5.977193880181603
141.03779834775494

 7. 总结文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

关于random模块的功能就 讲到这里,上面六种用法囊括了生成随机数的常用方法,如果想要使用更多的random模块中的方法,可以在Python帮助文档中进行查阅学习。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26227.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/26227.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定