Help with if statement

Jikosan jikosan at myrealbox.com
Sun Jan 11 03:37:51 EST 2004


Ya, I had mis-read the assignment.

The code is fully functional now. If anyone is curious, here it is.

import random   #import random number generator library
import math     #import math library

file=open('C:\Python\output.txt', 'w')
a, b = -1.0, 1.0    #set boundary conditions
for N in range(0, 100001, 25):   # 100,000 trials at steps of 25
    if N == 0:   #This corrects for N=0
        N = 1
    onecheck = 0
    for n in range(0,N): #generate (x,y) numbers N times.
        xrand = random.random()
        yrand = random.random()
        x = a * (1.0-(xrand)) + b*(xrand)
        y = a * (1.0-(yrand)) + b*(yrand)
        c = math.sqrt(math.pow(x,2.0)+math.pow(y,2.0))  #calculate distance
to origin
        if c < 1:
            onecheck = onecheck+1
    pi = 4.0*(onecheck*math.pow(N,-1))  #This is to calculate P(N), the
ratio of points whose
                                        #distance is less than 1 to the
total number of trials.
    if pi == 0.0: #This is to prevent 'log 0' errors
        pi == 1.0
    log = str(math.log(N, 10))
    file.write(str(math.log(N, 10)))
    file.write('    ')
    file.write(str(math.log(pi, 10)))
    file.write('\n')
file.close()





More information about the Python-list mailing list