Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import random
  2. import xlwt
  3.  
  4. wins=0
  5. attempts=500000
  6.  
  7. book=xlwt.Workbook(encoding="utf-8")
  8. sheet1=book.add_sheet("Results")
  9. sheet1.write(0, 0, "p")
  10. sheet1.write(0, 1, "wins")
  11. sheet1.write(0, 2, "attempts")
  12. i=1
  13.  
  14. while (p<=1):
  15.     wins=0
  16.     sheet1.write(i, 0, p)
  17.     for case in range(0, attempts):
  18.         score=0
  19.         for flip in range(0, 100):
  20.             a_expectancy=(99-flip)*(2*p-1)
  21.  
  22.             result=0
  23.             toss=random.uniform(0,1)
  24.             if (score+a_expectancy>=0):
  25.                 #go for coin A
  26.                 if (toss>=p): #tails
  27.                     result=-1
  28.                 else: #heads
  29.                     result=1
  30.  
  31.             else:
  32.                 #go for coin B
  33.                 if (toss>=0.5):
  34.                     result=2
  35.                 else:
  36.                     result=-2
  37.  
  38.             score+=result
  39.  
  40.         if (score>0):
  41.             wins+=1
  42.     sheet1.write(i, 1, wins)
  43.     sheet1.write(i, 2, attempts)
  44.     i+=1;
  45.     p+=0.05
  46. book.save("results.xls")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement