Help with if statement

J

Jikosan

I can't get my "if" statement to work indicated below.
What happens is no matter what the value random is, neg gets incremented by 1.

This is actually my first time programming in Python. Any help is appreciated.

TIA
Gene

import random
uniform = random.uniform
neg = 0
N = int(raw_input("Enter number of random generations, N: "))

for x in range(0,N):
random = uniform(-1, 1)
print random
if random < 1.0: <--- Not working
neg = neg+1
print neg
pi = 4.*(neg/N)
print 'The value of Pi is ', pi
 
G

Ganesan R

Jikosan" == Jikosan said:
I can't get my "if" statement to work indicated below. What happens is no
matter what the value random is, neg gets incremented by 1.

for x in range(0,N):
random = uniform(-1, 1)
print random
if random < 1.0: <--- Not working
neg = neg+1

Since uniform will return numbers in the interval [-1, 1), all instances of
random will be less than 1.0. May be you meant to say random < 0.0?

Ganesan
 
J

Jikosan

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()
 
J

Jikosan

I got it working. I had originally misread the prompt.

Here's the code I finally got working. It calculates PI using the Monte
Carlo method.
I have to graph log N vs log PI, which explains the last few lines of the
program.

Gene

#HW1 - 1.5.2
#This code was written in the Python programming language.

import random #Loads the random number generator library.
import math #Loads the math library.

file=open('output.txt', 'w') #Identifies output file.
a, b = -1.0, 1.0 #Sets the boundary conditions for (x,y).
for N in range(0, 100000, 25): #4000 trials, 1<= N <= 100000 at steps of
25.
if N == 0:
N = 1
onecheck = 0
for n in range(0,N): #This for loop generates (x,y) sets 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)) #Calculates the
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: #Prevent 'log 0' calculation errors
pi == 1.0

#Convert pi and N to log10 and write to a file to graph on Excel.
file.write(str(math.log(N, 10)))
file.write(' ')
file.write(str(math.log(pi, 10)))
file.write('\n')
file.close()
 
P

Paul Rubin

Jikosan said:
for N in range(0, 100000, 25):

For such a large list, you should xrange instead of range. That will
use less memory.
c = math.sqrt(math.pow(x,2.0) + math.pow(y,2.0))

You should use "c = math.sqrt(x*x + y*y)" which is both faster and
more flexible: math.pow(x,2.0) happens to work ok here, but can throw
an error in some implementations if x is negative.
pi = 4.0*(onecheck*math.pow(N,-1))

You can say "pi = (4.0 * onecheck) / N" here. That's faster and more
straightforward.
#Convert pi and N to log10 and write to a file to graph on Excel.

Why are you going to graph log(N) vs log(pi)? Do you expect them
to have an exponential relationship? If not, log(N) vs pi, instead
of log(pi), may make more sense.
 
J

Jikosan

Thanks Paul. I so far have only been learning Python from the tutorial and
greatly appreciate your tips.

|
| > #Convert pi and N to log10 and write to a file to graph on Excel.
|
| Why are you going to graph log(N) vs log(pi)? Do you expect them
| to have an exponential relationship? If not, log(N) vs pi, instead
| of log(pi), may make more sense.

Because my professor wants us to graph it log(N) vs log(pi).
Actually, it's log [P(N) - pi/4] vs log N. I misread the question again last
night.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,491
Latest member
mohitk

Latest Threads

Top