# What is the purpose of 1.0 in the last line?

Heres the problem: Sorry about the indentation. Here’s the code. I don’t understand the purpose of multiplying 1.0 with numberOfHits / NUMBER_OF_TRIALS. Does it have to do with decimal precision?

``````import random

NUMBER_OF_TRIALS = 1000000
numberOfHits = 0

for i in range(NUMBER_OF_TRIALS):
x = random.random() * 2.0 - 1;
y = random.random() * 2.0 - 1;
if x < 0:
numberOfHits += 1
elif not (x > 1 or x < 0 or y > 1 or y < 0):
slope = (1.0 - 0) / (0 - 1.0)
x1 = x + -y * slope
if x1 <= 1:
numberOfHits += 1

print("The probability in Region 1 and 3 is " +
str(1.0 * numberOfHits / NUMBER_OF_TRIALS))
``````

this is depends on your python version, given numberOfHits and NUMBER_OF_TRIALS are both integers, in python2, this would result in 0:

``````print 624542 / 1000000 # outputs 0 in python2
``````

given python2 rounds down if the division involves two integers

by including a float (`1.0`) in the equation, we ensure this doesn’t happen, so it ensures support for both python2 and python3 (without 1.0 it will work in python3 correctly, but not in python2)

1 Like

Thank you very much stetim94.

Also is there a random function which gives a random real number in the range [0, 1] including both 0 and 1.

Short answer, no, not if the desired outcome is a float. It is a simple matter to generate [0, 1],

``````randint(0,2)
``````

Something to consider…

``````>>> from random import random
>>> def rnd():
return random() * 11 / 10

>>> print (rnd())
0.9388749864478274
>>> print (rnd())
0.5615871213656047
>>> print (rnd())
0.5685012014457733
>>> print (rnd())
0.6331925532135552
>>> print (rnd())
0.014260045313360947
>>> print (rnd())
0.4796619820071058
>>> print (rnd())
0.6980231580612197
>>> print (rnd())
0.9758995970534003
>>> print (rnd())
0.2479411808051278
>>> print (rnd())
0.4251275501621959
>>> print (rnd())
0.4311769102905122
>>> print (rnd())
0.9946137239584611
>>> print (rnd())
0.2843607901671109
>>> print (rnd())
0.4127076092956984
>>> print (rnd())
0.5230884668505086
>>> print (rnd())
0.9992597355363472
>>> print (rnd())
1.0049627583563256
>>>
``````

The last one shows that we can reach and pass `1`. So it will up to our code to detect when it is equal or greater, and swap it out for 1.0 or convert to integer.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.