SHARE
TWEET

Untitled

a guest Feb 18th, 2012 40 Never
  1. import random
  2.  
  3. def randomperm(n):
  4.     x = range(n)
  5.     for i in xrange(n):
  6.         r = random.randint(i,n-1)
  7.         (x[i],x[r]) = (x[r], x[i])
  8.     return x
  9.  
  10. if __name__ == "__main__":
  11.     import sys
  12.     if len(sys.argv) < 2:
  13.         print "usage: python randomperm n"
  14.     n = int(sys.argv[1])
  15.     print randomperm(n)
RAW Paste Data
Top