Welcome, guest | Sign In | My Account | Store | Cart

Randomly shuffle elements of an array

Python, 11 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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

1 comment

Dennis Benzinger 19 years, 4 months ago  # | flag

How is this different from the shuffle function in the random module?

http://www.python.org/doc/2.4/lib/module-random.html