关于Python中随机数的使用案例

发布时间:2020-07-23 11:16:48 作者:清晨
来源:亿速云 阅读:168

不懂关于Python中随机数的使用案例?其实想解决这个问题也不难,下面让小编带着大家一起学习怎么去解决,希望大家阅读完这篇文章后大所收获。

在Python中使用随机性的概述,仅使用内置于标准库和CPython本身的功能。

Python随机数

生成介于0.0和1.0之间的随机浮点数

该random.random()函数在区间[0.0,1.0)中返回随机浮点数。这意味着返回的随机数将始终小于右侧端点(1.0)。这也称为半开放范围:

>>> import random
>>> random.random()
0.11981376476232541
>>> random.random()
0.757859420322092
>>> random.random()
0.7384012347073081

 在x和之间生成随机Intsy

这是如何使用该random.randint()函数在Python中的两个端点之间生成随机整数的方法。这跨越了整个[x,y]间隔,可能包括两个端点:

>>> import random
>>> random.randint(1, 10)
10
>>> random.randint(1, 10)
3
>>> random.randint(1, 10)
7

使用该random.randrange()功能,您可以排除间隔的右侧,这意味着生成的数字始终位于[x,y]内,并且它始终小于右端点:

>>> import random
>>> random.randrange(1, 10)
5
>>> random.randrange(1, 10)
3
>>> random.randrange(1, 10)
4

生成x和之间的随机浮点数y

如果需要生成位于特定[x,y]区间内的随机浮点数,则可以使用以下random.uniform函数:

>>> import random
>>> random.uniform(1, 10)
7.850184644194309
>>> random.uniform(1, 10)
4.00388600011348
>>> random.uniform(1, 10)
6.888959882650279

从列表中选取随机元素

要从非空序列(如列表或元组)中选择一个随机元素,您可以使用Python的random.choice函数:

>>> import random
>>> items = ['one', 'two', 'three', 'four', 'five']
>>> random.choice(items)
'five'
>>> random.choice(items)
'one'
>>> random.choice(items)
'four'

这适用于任何非空序列,但IndexError如果序列为空,它将抛出异常。

随机化元素列表

您可以使用该random.shuffle函数随机化序列。这将修改序列对象并随机化元素的顺序:

>>> import random
>>> items = ['one', 'two', 'three', 'four', 'five']
>>> random.shuffle(items)
>>> items
['four', 'one', 'five', 'three', 'two']

如果你不想改变原作,你需要先制作副本然后随机播放副本。您可以使用该copy模块创建Python对象的副本。

n从元素列表中选取随机样本

要从n序列中选择一个独特元素的随机样本,请使用该random.sample函数。它无需替换即可执行随机抽样:

>>> import random
>>> items = ['one', 'two', 'three', 'four', 'five']
>>> random.sample(items, 3)
['one', 'five', 'two']
>>> random.sample(items, 3)
['five', 'four', 'two']
>>> random.sample(items, 3)
['three', 'two', 'five']

生成密码安全随机数

如果出于安全考虑需要加密安全随机数,请使用random.SystemRandom加密安全伪随机数生成器。

SystemRandom该类的实例提供了作为random模块上的函数可用的大多数随机数生成器操作。这是一个例子:

>>> import random
>>> rand_gen = random.SystemRandom()
>>> rand_gen.random()
0.6112441459034399
>>> rand_gen.randint(1, 10)
2
>>> rand_gen.randrange(1, 10)
5
>>> rand_gen.uniform(1, 10)
8.42357365980016
>>> rand_gen.choice('abcdefghijklmn')
'j'
>>> items = ['one', 'two', 'three', 'four', 'five']
>>> rand_gen.shuffle(items)
>>> items
['two', 'four', 'three', 'one', 'five']
>>> rand_gen.sample('abcdefghijklmn', 3)
['g', 'e', 'c']

请注意,SystemRandom并不保证在运行Python的所有系统上都可以使用(尽管通常会这样。)

Python 3.6+ - secrets模块:

如果您正在使用Python 3并且您的目标是生成加密安全随机数,那么请务必查看该secrets模块。该模块在Python 3.6(及更高版本)标准库中提供。它使得生成安全令牌变得轻而易举。

这里有一些例子:

>>> import secrets
# Generate secure tokens:
>>> secrets.token_bytes(16)
b'\xc4\xf4\xac\x9e\x07\xb2\xdc\x07\x87\xc8 \xdf\x17\x85^{'
>>> secrets.token_hex(16)
'a20f016e133a2517414e0faf3ce4328f'
>>> secrets.token_urlsafe(16)
'eEFup5t7vIsoehe6GZyM8Q'
# Picking a random element from a sequence:
>>> secrets.choice('abcdefghij')
'h'
# Securely compare two strings for equality
# (Reduces the risk of timing attacks):
>>> secrets.compare_digest('abcdefghij', '123456789')
False
>>> secrets.compare_digest('123456789', '123456789')
True

感谢你能够认真阅读完这篇文章,希望小编分享关于Python中随机数的使用案例内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!

推荐阅读:
  1. Python中super()方法的使用案例
  2. python中flag的使用案例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:C# .NET及Mono怎么实现跨平台

下一篇:tomcat8启动占内存溢出

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》