Python - 具有不断变化的变量值的递归函数调用



我正在尝试创建一个函数,该函数使用蒙特卡罗近似方法来计算给定精度(小数位数(的 pi 值。我试图通过将近似值与 pi 的文字值进行比较来做到这一点,如果它们不匹配,则递归调用函数以再次尝试进行更多的试验。这是我到目前为止所拥有的:

def estimate_pi(precision):
     x = 1
     N_tot = 0
     N_hits = 0
     for i in range(0,trials*10**x):
         if(inside_circle(random.random(),random.random())==True):
             N_hits = N_hits+1
             N_tot = N_tot+1
         else:
             N_tot = N_tot+1
     result = 4.0*N_hits/N_tot
     if compare_to_pi(result,precision)==True:
         print(result)
     else:
         x++
         estimate_pi(precision)
         if trials>999999:
             raise Error('approximation not converging to given precision')
             print(result)

我遇到了麻烦,因为变量 x 应该控制近似的试验次数,每次调用函数时都会初始化为 1。我不知道该怎么办,请帮忙!

您可以通过

将函数包装在类中并让类实例存储 x 随时间变化的值来解决此问题。

class foo():
   def __init__(self, precision, trials):
       self.x = 1
       self.precision = precision
       self.trials = trials
       ....
   def compare_to_pi(self, ...):
       ....
   def inside_circle(self, ...):
       ....
   def estimate_pi(self, precision):
       N_tot = 0
       N_hits = 0
       for i in range(0, self.trials*10**self.x):
           if(inside_circle(random.random(),random.random())==True):
               N_hits = N_hits+1
               N_tot = N_tot+1
          else:
               N_tot = N_tot+1
      result = 4.0*N_hits/N_tot
      if compare_to_pi(result, self.precision)==True:
          print(result)
      else:
          self.x += 1
          self.estimate_pi(self.precision)
      if self.trials > 999999:
         raise Error('approximation not converging to given precision')
         print(result)

您需要一个用于此递归序列的帮助程序方法。这是您当前的方法

def estimate_pi(precision):
     x = 1
     N_tot = 0
     N_hits = 0
     for i in range(0,trials*10**x):
         if(inside_circle(random.random(),random.random())==True):
             N_hits = N_hits+1
             N_tot = N_tot+1
         else:
             N_tot = N_tot+1
     result = 4.0*N_hits/N_tot
     if compare_to_pi(result,precision)==True:
         print(result)
     else:
         x++
         estimate_pi(precision)
         if trials>999999:
             raise Error('approximation not converging to given precision')
             print(result)

我将用

def estimate_pi(precision):
 x = 1
 N_tot = 0
 N_hits = 0
 for i in range(0,trials*10**x):
     if(inside_circle(random.random(),random.random())==True):
         N_hits = N_hits+1
         N_tot = N_tot+1
     else:
         N_tot = N_tot+1
 result = 4.0*N_hits/N_tot
 if compare_to_pi(result,precision)==True:
     print(result)
 else:
     x++
     estimate_pi_helper(precision, 2) #we are on our second trial

def estimate_pi_helper(precision,trials, N_tot, N_hits):
    if trials>999999: #if we reach our 1000000'th trial we throw an error
         raise Error('approximation not converging to given precision')
         print(result)

 for i in range(0,trials*10**x):
     if(inside_circle(random.random(),random.random())==True):
         N_hits = N_hits+1
         N_tot = N_tot+1
     else:
         N_tot = N_tot+1
 result = 4.0*N_hits/N_tot
 if compare_to_pi(result,precision)==True:
     print(result)
 else:
     x++
     estimate_pi_helper(precision,trials+1, N_tot, N_hits)

我把你的x固定为你的试验。我还添加了一个帮助程序方法,该方法将试验作为参数来保持运行计数。最后,您的命中数和总数被传入以保持运行计数,我的印象是这就是蒙特卡罗模拟的完成方式,尽管我可能错了。如果您需要每次运行的新命中数和总数,请相应地更改它。

很抱歉缩进问题。IDE应该很容易修复它

最新更新