Python 中随机播放的随机性
Randomness of Shuffle in Python
我指的是 Python 随机模块的随机播放功能。
from random import shuffle
list = [1, 2, 3, 4]
shuffle(list)
我猜上面的函数使用了随机种子。我知道在 C 语言中,rand 函数会迭代计算机中的一些随机种子数。因此,当循环函数时,随机函数不再随机。
shuffle 函数的工作方式是否类似于 C 中的 rand 函数?如果是这样,我如何添加自己的随机种子? (我正在考虑使用以毫秒为单位的时间来得出唯一值)。
之前对这个问题的已接受答案发表了评论,但无法得到任何回复 (Shuffling a list of objects in python)
编辑:
我想确保随机洗牌不会在循环中重复其洗牌方法。例如,
我想随机播放 [1, 2, 3, 4, 5, 6]
我循环了10000多次
它产生以下结果:
[1, 3, 2, 4, 5, 6]
[2, 1, 4, 5, 3, 6]
...(大量不同组合的洗牌)
[1, 3, 2, 4, 5, 6]
[2, 1, 4, 5, 3, 6]
...(重复模式)。
我想避免上述行为,因为我正在循环处理大量数据。上述行为会首先发生吗?如果是这样,我是否必须在循环一定次数后更改种子?
Almost all module functions depend on the basic function random(), which
generates a random float uniformly in the semi-open range [0.0, 1.0).
Python uses the Mersenne Twister as the core generator. It produces
53-bit precision floats and has a period of 2**19937-1. The underlying
implementation in C is both fast and threadsafe. The Mersenne Twister
is one of the most extensively tested random number generators in
existence. However, being completely deterministic, it is not suitable
for all purposes, and is completely unsuitable for cryptographic
purposes.
The random module also provides the SystemRandom class which uses the
system function os.urandom() to generate random numbers from sources
provided by the operating system.
Warning The pseudo-random generators of this module should not be used
for security purposes. Use os.urandom() or SystemRandom if you require
a cryptographically secure pseudo-random number generator.
I know that in C, rand function iterates over a few random seed numbers in computer.
不,不是。它使用您使用 srand
设置的任何种子,如果您没有调用 srand
.
,则使用 1
Does shuffle function work similar to rand function in C? If so, how can I add my own seed that is random? (I am thinking of using time in millisecond to come up with unique value).
Python's random
module uses either system time or OS-provided randomness sources to seed the RNG:
random.seed([x])
Initialize the basic random number generator. Optional argument x can
be any hashable object. If x is omitted or None
, current system time
is used; current system time is also used to initialize the generator
when the module is first imported. If randomness sources are provided
by the operating system, they are used instead of the system time (see
the os.urandom()
function for details on availability).
Changed in version 2.4: formerly, operating system resources were not
used.
我指的是 Python 随机模块的随机播放功能。
from random import shuffle
list = [1, 2, 3, 4]
shuffle(list)
我猜上面的函数使用了随机种子。我知道在 C 语言中,rand 函数会迭代计算机中的一些随机种子数。因此,当循环函数时,随机函数不再随机。
shuffle 函数的工作方式是否类似于 C 中的 rand 函数?如果是这样,我如何添加自己的随机种子? (我正在考虑使用以毫秒为单位的时间来得出唯一值)。
之前对这个问题的已接受答案发表了评论,但无法得到任何回复 (Shuffling a list of objects in python)
编辑:
我想确保随机洗牌不会在循环中重复其洗牌方法。例如,
我想随机播放 [1, 2, 3, 4, 5, 6]
我循环了10000多次
它产生以下结果: [1, 3, 2, 4, 5, 6] [2, 1, 4, 5, 3, 6] ...(大量不同组合的洗牌) [1, 3, 2, 4, 5, 6] [2, 1, 4, 5, 3, 6] ...(重复模式)。
我想避免上述行为,因为我正在循环处理大量数据。上述行为会首先发生吗?如果是这样,我是否必须在循环一定次数后更改种子?
Almost all module functions depend on the basic function random(), which generates a random float uniformly in the semi-open range [0.0, 1.0). Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1. The underlying implementation in C is both fast and threadsafe. The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.
The random module also provides the SystemRandom class which uses the system function os.urandom() to generate random numbers from sources provided by the operating system.
Warning The pseudo-random generators of this module should not be used for security purposes. Use os.urandom() or SystemRandom if you require a cryptographically secure pseudo-random number generator.
I know that in C, rand function iterates over a few random seed numbers in computer.
不,不是。它使用您使用 srand
设置的任何种子,如果您没有调用 srand
.
Does shuffle function work similar to rand function in C? If so, how can I add my own seed that is random? (I am thinking of using time in millisecond to come up with unique value).
Python's random
module uses either system time or OS-provided randomness sources to seed the RNG:
random.seed([x])
Initialize the basic random number generator. Optional argument x can be any hashable object. If x is omitted or
None
, current system time is used; current system time is also used to initialize the generator when the module is first imported. If randomness sources are provided by the operating system, they are used instead of the system time (see theos.urandom()
function for details on availability).Changed in version 2.4: formerly, operating system resources were not used.