简体   繁体   中英

Python - recursive function call with changing variable value

I'm trying to create a function that uses the Monte Carlo approximation method to calculate the value of pi to the given precision (number of decimal places). I was trying to do this by comparing the approximated value to the literal value of pi and if they don't match, recursively calling the function on itself to try again with a greater number of trials. This is what I have so far:

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)

I'm having trouble because the variable x, which is supposed to control the number of trials for approximation, gets initialised to 1 every time the function is called. I have no idea what to do, please help!

you can solve this by wrapping your function in a class and have the class instance store the value of x over time.

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)

You need a helper method for this recursive sequence. This is your current method

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)

I'll replace this with

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)

I fixed your x to be your trials. I also added a helper method that takes trials as a parameter to keep a running count. Finally, your hits and totals are passed in to keep a running count, I Was under the impression that this is how a Monte Carlo simulation is done although I'm probably wrong. If you need a new set of hits and total on each run then please change it accordingly.

Sorry for the indentation issues. IDE should fix it easily

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM