# NumPy Random Permutation (Python Tutorial)

This is a detailed tutorial of NumPy Random Permutation. Learn to create NumPy Arrays with random permutations with the examples.

## Random Permutations

Permutation refers to the setup for the elements where we have various combinations. In these combinations, we have given a set of numbers in which all the combinations will be given. As a result, we will get a set random number which will have the same number as we have specified but in different combinations. For example, we have an array as [8,7,6], and also we have can have other permutations as [7,8,6] and also [6,7,8] is also another combination.

The NumPy module has two methods for this permutations:

• `shuffle()`
• `permutation()`

### Shuffling Arrays:

What we exactly do while is shuffling is changing places of the elements in the arrays. In this, we change the positions of the elements in the array with respect to our needs. But this arrangement takes place in the array itself, not outside the array.

Let us take an example where we will try to shuffle up things randomly:

```#Importing the numpy package and also the random module.
from numpy import random
# here we will import the numpy by making an alias we need to shuffle.
import numpy as np
#Now we assign probability to the numbers by using choice()
array1 = np.array([9,8,7,6,5,4])
#Now we will use the shuffle method.
random.shuffle(array1)
# we will print the array
print(array1)```

Output.

`[8 6 5 9 7 4]`

Here we get an array whose elements are completely shuffled from its previous values as [9,8,7,6,5,4] is shuffled into this [8 6 5 9 7 4].

Let us go through another example:

```#Importing the numpy package and also the random module.
from numpy import random
# here we will import the numpy by making an alias we need to shuffle.
import numpy as np
#Now we assign probability to the numbers by using choice()
array1 = np.array([11,12,13,14,15,16,17,18])
#Now we will use the shuffle method.
random.shuffle(array1)
# we will print the array
print(array1)```

Output.

`[11 12 14 18 15 13 16 17]`

Here also we get a shuffled array, but we realize on the thing that all the changes are done to the original array, and the changes are permanent.

### Permutation Method

These methods work on the problem we have with the shuffle. As here we work on a separate copy of the array, and no changes are done to the original array. It also returns a re-arranged array of elements.

Let us go through an example:

```#Importing the numpy package and also the random module.
from numpy import random
# here we will import the numpy by making an alias we need to shuffle.
import numpy as np
#Now we assign probability to the numbers by using choice()
array1 = np.array([9,8,7,6,5,4])
#Now we will use the permutation method.
# we will print the array
print(random.permutation(array1))```

Output.

```[8 6 5 9 7 4]
[5 9 4 7 6 8]
[5 6 7 4 9 8]
[6 4 5 7 9 8]```

Let us go through another example:

```#Importing the numpy package and also the random module.
from numpy import random
# here we will import the numpy by making an alias we need to shuffle.
import numpy as np
#Now we assign probability to the numbers by using choice()
array1 = np.array([21,22,23,24,25,26,27,28])
#Now we will use the method.
# we will print the array
print(random.permutation(array1))```

Output.

`[21 22 24 28 25 23 26 27]`

Here we get a separate copy of the array, and it is all reassembled.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!