Guest User

Untitled

a guest
Dec 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. import math,random
  2.  
  3. def testStatistic(x,n,p):
  4. q = 1-p
  5. return(x/n - p)/(math.sqrt(p*q/n))
  6.  
  7. max = 10000
  8. sampleSize = 1000
  9.  
  10. #the right way
  11. count = 0
  12. for i in range(max):
  13. x = 0
  14. for j in range(sampleSize):
  15. x+=random.randint(0,1)
  16. if testStatistic(x,sampleSize,.5) > 1.2816: count += 1
  17. print('good: '+str(count/max))
  18.  
  19. #the wrong way
  20. count = 0
  21. for i in range(max):
  22. x= 0
  23. trials = 0
  24. while (trials < sampleSize):
  25. x, trials = x+random.randint(0,1),trials + 1
  26. if x > 15 and trials - x > 15:
  27. if testStatistic(x,trials,.5)>1.2816:
  28. count +=1
  29. trials = sampleSize
  30. print('bad: '+str(count/max))
Add Comment
Please, Sign In to add comment