Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1.  
  2. #Assignment2pt2
  3. #Jonathan Dombrowski
  4. #916921673
  5. #Jdombrowski416@gmail.com
  6.  
  7. import numpy as np
  8. import matplotlib.pyplot as plt
  9. numberOfTrials = 10000000
  10. results = np.zeros((numberOfTrials),dtype=float)
  11. temp1=0.0
  12. temp2=0.0
  13. trialCounter=0
  14.  
  15. def fillList():
  16.     for i in range(numberOfTrials):
  17.         global trialCounter
  18.         trialCounter=1
  19.         temp1 = np.random.uniform(0,1)
  20.         temp2 = np.random.uniform(0,1)
  21.         results[i]=recGenerator(temp1,temp2)
  22.  
  23. def recGenerator(temp1,temp2):
  24.     global trialCounter
  25.     if(temp1>temp2):
  26.         pass
  27.     else:
  28.         temp1=temp2
  29.         temp2=np.random.uniform(0,1)
  30.         trialCounter+=1
  31.         recGenerator(temp1,temp2)
  32.     return trialCounter
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. def findExpectedValue():
  44.     sum =0.0
  45.     for i in range(numberOfTrials):
  46.         sum += results[i]
  47.     return str(sum/numberOfTrials)
  48. fillList()
  49. print("Expected number of trials is : "+findExpectedValue())
  50. plt.hist(results, bins=7,normed=1,cumulative=False,
  51.             rwidth=1,stacked=False,label="dataset 1",log=False)
  52. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement