Advertisement
canorve

pick random numbers >1, average is number e

Oct 15th, 2021
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3.  
  4. import numpy as np
  5.  
  6.  
  7. class PickRandom():
  8.  
  9.     def RandSum1(self):
  10.         s=0
  11.         a = np.array([])
  12.         while(s < 1):
  13.             a = np.append(a,np.random.rand())
  14.             s = np.sum(a)
  15.  
  16.         return len(a)
  17.  
  18.     def NumAve(self,n):
  19.  
  20.         N = np.arange(n)
  21.         l=np.array([])
  22.         for idx,item in enumerate(N):
  23.            
  24.             l = np.append(l,self.RandSum1())
  25.  
  26.         return np.mean(l)
  27.  
  28.  
  29. n = 10000 # number of times to select the set of random numbers > 1  
  30. print("number e =",PickRandom().NumAve(n))
  31.  
  32.  
  33.  
  34.  
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement