Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Shuffle an Array in Python



Shuffling an array involves rearranging the elements of the array into a random order. For example, if arr = [1, 2, 3, 4, 5], the output might be [5, 3, 2, 1, 4]. Let's explore different ways to shuffle an array in Python. Following are the various methods to shuffle an array in Python ?

Using shuffle() Method

The shuffle() method in numpy module is used to rearrange the elements of an array. For this, we need to import numpy module using the import keyword.

Example

In the following example, we have shuffled the elements of an array using the shuffle() method ?

# Import required module
import numpy as np
# Assign array
arr = np.array([11, 22, 33, 44, 55, 66])
# Display original array
print("Original array : ", arr)
# Shuffle array
np.random.shuffle(arr)
# Display shuffled array
print("Shuffled array : ", arr)

Following is the output of the above code ?

Original array :  [11 22 33 44 55 66]
Shuffled array :  [44 33 66 55 11 22]

Using permutation() Method

The Numpy module consists of numpy.random.permutation() method, which returns a shuffled copy of an array. we need to import numpy module for this method.

Example

Following is an example of shuffling the elements of an array using the permutation module ?

# Import required module
import numpy as np
# Assign array
arr = [10, 20, 30, 40, 50, 60]
# Display original array
print("Original array : ", arr)
# Shuffle array
shuffle_array = np.random.permutation(arr)
# Display shuffled array
print("Shuffled array : ", shuffle_array)

Following is the output of the above code ?

Original array :  [10, 20, 30, 40, 50, 60]
Shuffled array :  [20 50 60 30 10 40]

Using simple() Method

The simple() method in the random module shuffles the array elements. For this, we need to import a random module using the import keyword.

Example

Here, we have shuffled the elements of an array using the simple() method from a random module ?

# Import required module
import random
arr = [1, 2, 3, 4, 5, 6]
# Display original array
print("Original array : ", arr)
# Shuffle array
arr = (random.sample(list(arr), 6))
# Display shuffled array
print("Shuffled array : ", arr)

Here is the output of the above code ?

Original array :  [1, 2, 3, 4, 5, 6]
Shuffled array :  [5, 4, 2, 3, 6, 1]

Shuffling using random indices

We can shuffle the array by random indices, for this method we will randomly select an index and append it to the end of the array. this will be repeated for n time where n is the length of the array.

Algorithm

Following are the steps to shuffle an array using random indices ?

  • Initialize an array
  • Iterate the for loop over the array for n-1 times, where n is the length of the array
  • select the random index
  • Pop (remove) the value at that random index from the array
  • Append the popped value to the end of the array
  • After iterating the loop, the array will be shuffled

Example

In the following example we have shuffled the elements of the array selecting random indices ?

import random
arr=[1,2,3,4,5,6]
print("Original Array :",arr)
n=len(arr)-1
for i in range(n):
   random_index = random.randint(0, n)
   temp = arr.pop(random_index)
   arr.append(temp)
print("Shuffled array :",arr)

Following is the output of the above code ?

Original Array : [1, 2, 3, 4, 5, 6]
Shuffled array : [1, 3, 5, 6, 4, 2]

Fisher-Yates shuffle Algorithm

The Fisher-Yates shuffle method is used to shuffle the elements of the array. In this method, we will randomly select the index and swap the elements.

Algorithm

Following are the steps to shuffle the elements of an array using Fisher-Yates ?

  • Initialize the array and determine its length, n
  • Start iterating through an array from the last index (n-1) and move toward the first index (0)
  • For each iteration, pick a random index j between 0 and the current index i
  • Swap the element at index i with the element at the randomly selected index j
  • Continue until all elements have been processed
  • After completing the loop, the array will be shuffled randomly

Example

In the following example, we have shuffled the array using the Fisher-Yates method ?

# permutation of array
import random
import numpy as np
# permutation of array
def shuffler (my_array, n):
	# We will Start from the last element 
	# and swap one by one.
	for i in range(n-1,0,-1):
		# Pick a random index from 0 to i
		j = random.randint(0,i+1)
		# Swap arr[i] with the element at random index
		my_array[i],my_array[j] = my_array[j],my_array[i]
	return my_array
	
# Assign array

my_array = np.array([10, 20, 30, 40, 50, 60])
# Display original array
print("Original array: ",my_array)
# Get length of array
n = len(my_array)
# Use shuffler() function to get shuffled array
print("Shuffled array: ",shuffler(my_array, n))

Following is the output of the above code ?

Original array:  [10 20 30 40 50 60]
Shuffled array:  [60 50 20 30 10 40]
Updated on: 2025-01-07T11:15:58+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements