Advertisement
Guest User

Untitled

a guest
Aug 21st, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. import random
  2. import datetime
  3.  
  4. start = datetime.datetime.now()
  5.  
  6. runs = 10 ** 6
  7. i = 0
  8. seed = datetime.datetime.utcnow()
  9. seed = seed.hour * seed.minute + seed.second
  10. random.seed(seed)
  11. sequence = []
  12.  
  13. stack = [0,1,0,1,0,1,0,1,0,1]
  14.  
  15. def normal():
  16.     return 0
  17.  
  18. def rand():
  19.     return random.randint(0,1)
  20.  
  21. def regress():
  22.     s = sum(stack[-4:])
  23.     if s == 2# coin flip is the only fair thing
  24.         return random.randint(0,1)
  25.     elif s > 2:
  26.         return 0
  27.     else:
  28.         return 1
  29.  
  30. def regressLong():
  31.     s = sum(stack)
  32.     if s == 5# coin flip is the only fair thing
  33.         return random.randint(0,1)
  34.     elif s > 5:
  35.         return 0
  36.     else:
  37.         return 1
  38.  
  39. jumpy = [('normal', normal), ('rand', rand), ('regress', regress), ('regressLong', regressLong)]
  40.  
  41.  
  42. results = dict(normal=0,rand=0,regress=0,regressLong=0)
  43.  
  44. last = 0
  45. for x in range(0, runs):
  46.     y = random.randint(0,1)
  47.     for n,f in jumpy:
  48.         if f() == y:
  49.             results[n] += 1
  50.  
  51.     i += y
  52.     last = y
  53.     stack.append(y)
  54.     stack = stack[1:]
  55.  
  56.     if x % 100000 == 0:
  57.         print 'Done %i runs' % x
  58.  
  59. for k,v in results.iteritems():
  60.     print 'Variable {k} was successful {t} of the time'.format(k=k, t=v*100.0/runs)
  61.  
  62. print(i)
  63.  
  64. end = datetime.datetime.now()
  65.  
  66. print end - start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement