Skip to content
It's Algorithm PowerCatalogMy algorithmsNew

Fisher Yates Shuffle

Open interactive visualization โ†’
algorithms

Randomly shuffle elements of an array


Python
1 2 3 4 5 6 7 8 9 10 11 12
import random

def shuffle(ary):
    a=len(ary)
    b=a-1
    for d in range(b,0,-1):
      e=random.randint(0,d)
      if e == d:
            continue
      ary[d],ary[e]=ary[e],ary[d]
    return ary

code.activestate.com โ†—

Algorithm PowerBack to top โ†‘