Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 什么时候我应该选择random.choice()而不是random.randint?_Python_Random - Fatal编程技术网

Python 什么时候我应该选择random.choice()而不是random.randint?

Python 什么时候我应该选择random.choice()而不是random.randint?,python,random,Python,Random,考虑以下两个选项以从消息列表打印随机字符串: import random messages = ['It is certain', 'It is decidedly so', 'Yes definitely', 'Reply hazy try again', 'Ask again later', 'Concentrate and ask again', 'My reply is no', 'Outlook not so good',

考虑以下两个选项以从消息列表打印随机字符串:

import random

messages = ['It is certain',
    'It is decidedly so',
    'Yes definitely',
    'Reply hazy try again',
    'Ask again later',
    'Concentrate and ask again',
    'My reply is no',
    'Outlook not so good',
    'Very doubtful']


print(messages[random.randint(0, len(messages) - 1)])  # option 1

print(random.choice(messages))                         # option 2
选项1和选项2产生相同的效果。有没有理由选择其中一个而不是另一个?性能如何?一个比另一个更有效?

一些时间分析:

from timeit import timeit

setup = """
import random
import string
s = string.ascii_lowercase
x = [s[:random.randint(1, 26)] for _ in range(10_000)]
"""
stmt1 = """
random.choice(x)
"""

stmt2 = """
x[random.randint(0, len(x) - 1)]
"""

for i in range(0, 100_000, 10_000):
    print(f"Size_seq: {i}")
    print(f"    choice: {timeit(stmt = stmt1, setup = setup, number = i):>.5}s")
    print(f"    randint: {timeit(stmt = stmt2, setup = setup, number = i):>.5}s")
输出:

Size_seq: 0
    choice: 1.426e-06s
    randint: 1.672e-06s
Size_seq: 10000
    choice: 0.010363s
    randint: 0.017393s
Size_seq: 20000
    choice: 0.020168s
    randint: 0.034601s
Size_seq: 30000
    choice: 0.030942s
    randint: 0.050896s
Size_seq: 40000
    choice: 0.041753s
    randint: 0.069362s
Size_seq: 50000
    choice: 0.053554s
    randint: 0.088815s
Size_seq: 60000
    choice: 0.067187s
    randint: 0.10979s
Size_seq: 70000
    choice: 0.080606s
    randint: 0.12793s
Size_seq: 80000
    choice: 0.083252s
    randint: 0.1365s
Size_seq: 90000
    choice: 0.091724s
    randint: 0.14603s

这种差异在很大程度上是显著的。因此,如果输入量不大,但randint的速度明显慢于random.choice,则可以使用任意一种。当大小增加时,选择。

这取决于偏好。如果你喜欢选项1,那么就去做,反之亦然


但当涉及到其他人阅读时,我更希望你使用选项2。它只是让我和其他人很容易。。。您也可能在一两周后再次使用它,可能会被困一分钟,看看您做了什么。

因此,
random。choice
仅在内部调用
\u randbown
,这取决于实现

另一方面,
random.randint
random.randrange
的包装器,用于查找起点和终点之间的数字。它检查了很多东西,因为提供了2个数字,如果它们被切换和填充,可能会很麻烦,而且它最后还使用了
\u randbown
,所以它和随机一样好。在最佳情况下选择

TLDR;同样的随机性,但选择应该是最快的(几乎无法区分),并且更具可读性


我的声明来源:

你是根据可读性来选择的,Ex 2对我来说更清楚,因为我不知道如何阅读你写的东西。我不熟悉这个,但是你的例子2似乎经历了更多的东西?所以执行可能需要更长的时间?那么,区别只是方便吗?是的,我会坚持使用random.choice()当然!第二个块始终具有
Size:{Size}
,其中Size是选择随机元素的数组的大小。正如回答者所说,在小型阵列上差异最小,但90000单元阵列的差异为0.05秒,即
1/20
秒。这样做20次,你会得到20秒的差异