Answers for "Shuffle a given array of elements (Fisher–Yates shuffle)"

0

Shuffle a given array of elements (Fisher–Yates shuffle)

from random import randrange
 
 
# Utility function to swap elements `A[i]` and `A[j]` in a list
def swap(A, i, j):
 
    temp = A[i]
    A[i] = A[j]
    A[j] = temp
 
 
# Function to shuffle a list `A`
def shuffle(A):
 
    # read list from the lowest index to highest
    for i in range(len(A) - 1):
        # generate a random number `j` such that `i <= j < n`
        j = randrange(i, len(A))
 
        # swap the current element with the randomly generated index
        swap(A, i, j)
Posted by: Guest on February-02-2021

Code answers related to "TypeScript"

Browse Popular Code Answers by Language