Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import xlwt
- wins=0
- attempts=500000
- book=xlwt.Workbook(encoding="utf-8")
- sheet1=book.add_sheet("Results")
- sheet1.write(0, 0, "p")
- sheet1.write(0, 1, "wins")
- sheet1.write(0, 2, "attempts")
- i=1
- while (p<=1):
- wins=0
- sheet1.write(i, 0, p)
- for case in range(0, attempts):
- score=0
- for flip in range(0, 100):
- a_expectancy=(99-flip)*(2*p-1)
- result=0
- toss=random.uniform(0,1)
- if (score+a_expectancy>=0):
- #go for coin A
- if (toss>=p): #tails
- result=-1
- else: #heads
- result=1
- else:
- #go for coin B
- if (toss>=0.5):
- result=2
- else:
- result=-2
- score+=result
- if (score>0):
- wins+=1
- sheet1.write(i, 1, wins)
- sheet1.write(i, 2, attempts)
- i+=1;
- p+=0.05
- book.save("results.xls")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement