您可以检查它的功能:def shuffle(self, x, random=None):
"""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.
"""
if random is None:
randbelow = self._randbelow
for i in reversed(range(1, len(x))):
# pick an element in x[:i+1] with which to exchange x[i]
j = randbelow(i+1)
x[i], x[j] = x[j], x[i]
else:
_int = int
for i in reversed(range(1, len(x))):
# pick an element in x[:i+1] with which to exchange x[i]
j = _int(random() * (i+1))
x[i], x[j] = x[j], x[i]
else分支在这里应用,尤其是j = _int(random() * (i+1))行。这是经典的Fisher-Yates shuffle
所以随机值的“使用粒度”通常取决于列表的长度,尤其是当前元素的索引。在
哦,顺便说一句:你不是在提供一个种子,而是在提供随机值——最终每个迭代都是一个值。
相反,您可以使用随机值进行一次随机洗牌:
^{pr2}$
或者,您也可以使用seed-s,但实际上它们不应该是介于0和1之间的浮点数,请参阅def seed(self, a=None, version=2):
"""Initialize internal state from hashable object.
None or no argument seeds from current time or from an operating
system specific randomness source if available.
If *a* is an int, all bits are used.
For version 2 (the default), all of the bits are used if *a* is a str,
bytes, or bytearray. For version 1 (provided for reproducing random
sequences from older versions of Python), the algorithm for str and
bytes generates a narrower range of seeds.
"""
if version == 1 and isinstance(a, (str, bytes)):
x = ord(a[0]) << 7 if a else 0
for c in a:
x = ((1000003 * x) ^ ord(c)) & 0xFFFFFFFFFFFFFFFF
x ^= len(a)
a = -2 if x == -1 else x
if version == 2 and isinstance(a, (str, bytes, bytearray)):
if isinstance(a, str):
a = a.encode()
a += _sha512(a).digest()
a = int.from_bytes(a, 'big')
super().seed(a)
self.gauss_next = None
父类(其中a结束,有或没有这些损坏过程)是本机代码:NUMBA_EXPORT_FUNC(PyObject *)
_numba_rnd_seed(PyObject *self, PyObject *args)
{
unsigned int seed;
rnd_state_t *state;
if (!PyArg_ParseTuple(args, "O&I:rnd_seed",
rnd_state_converter, &state, &seed)) {
/* rnd_seed_*(bytes-like object) */
Py_buffer buf;
PyErr_Clear();
if (!PyArg_ParseTuple(args, "O&s*:rnd_seed",
rnd_state_converter, &state, &buf))
return NULL;
if (rnd_seed_with_bytes(state, &buf))
return NULL;
else
Py_RETURN_NONE;
}
else {
/* rnd_seed_*(int32) */
numba_rnd_init(state, seed);
Py_RETURN_NONE;
}
}
甚至可能发生这样的情况:传递一个浮点值会导致在这里运行最后一个分支,在这里使用未初始化的seed值,导致行为一致,这仅仅是因为在调用这个_numba_rnd_seed之前的函数调用使堆栈处于相同的状态。在