numpy.random.RandomState.shuffle#

方法

random.RandomState.shuffle(x)#

透過打亂其內容來就地修改序列。

此函數僅沿多維陣列的第一軸打亂陣列。子陣列的順序會變更,但其內容保持不變。

注意

新程式碼應改用 shuffle 方法,此方法屬於 Generator 實例;請參閱快速入門

參數:
xndarray 或 MutableSequence

要打亂的陣列、列表或可變序列。

返回:
None

參見

random.Generator.shuffle

新程式碼應使用的方法。

範例

>>> arr = np.arange(10)
>>> np.random.shuffle(arr)
>>> arr
[1 7 5 2 9 4 3 6 0 8] # random

多維陣列僅沿第一軸打亂

>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.shuffle(arr)
>>> arr
array([[3, 4, 5], # random
       [6, 7, 8],
       [0, 1, 2]])