megengine.random.permutation

permutation(n, *, dtype='int32')
Randomly permute a sequence, or return a permuted range.

If n is a multi-dimensional tensor, it is only shuffled along its first index.

Parameters
  • n (Union[int, Tensor]) – If n is an integer, random permutation of integers from \(0\) to \(n - 1\). If n is an tensor, make a copy and shuffle the elements randomly.

  • dtype (str) – the output data type when n is an integer. int32, int16 and float32 are supported. Default: int32

Returns

The output tensor.

Examples

>>> import numpy as np
>>> import megengine.random as rand
>>> x = rand.permutation(10, dtype="int32")
>>> x.numpy()   
array([8, 4, 0, 3, 5, 6, 2, 1, 7, 9], dtype=int32)
>>> x = rand.permutation(10, dtype="float32")
>>> x.numpy()   
array([1., 3., 0., 2., 4., 8., 7., 9., 6., 5.], dtype=float32)
>>> x = mge.tensor(np.arange(18)).reshape(6,3)
>>> x = rand.permutation(x)
>>> x.numpy()   
array([[15, 16, 17],
       [ 6,  7,  8],
       [ 0,  1,  2],
       [ 3,  4,  5],
       [12, 13, 14],
       [ 9, 10, 11]], dtype=int32)