daily pastebin goal
37%
SHARE
TWEET

tests

a guest Oct 30th, 2013 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import randrange
  2.  
  3. l = 10
  4. t = 50000
  5.  
  6. def shuffle_from_pos(arr):
  7.     for i in xrange(l):
  8.         j = randrange(i, l)
  9.         arr[i], arr[j] = arr[j], arr[i]
  10.     return arr
  11.  
  12. # copy pased from:
  13. # http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Sattolo.27s_algorithm
  14. def sattoloCycle(items):
  15.     i = len(items)
  16.     while i > 1:
  17.         i = i - 1
  18.         j = randrange(i)  # 0 <= j <= i-1
  19.         items[j], items[i] = items[i], items[j]
  20.     return items
  21.  
  22. def test(f):
  23.     rv = [0]*l
  24.     for i in xrange(t):
  25.         rv = map(sum, zip(rv, f(range(l))))
  26.     return rv
  27.  
  28. def normalize(arr):
  29.     s = float(sum(arr))
  30.     return map(lambda a: "%.4f" % (a/s), arr)
  31.  
  32. print "each element can be moved to places after it or stay still"
  33. print normalize(test(shuffle_from_pos))
  34. print "Sattolo's algorithm, copy pasted from wiki"
  35. print normalize(test(sattoloCycle))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top