Style
PEP8 is the Python style guide and is worth a read to keep your code clear and understandable.
Naming
You're not using your loop indicies, t
and i
, it's customary to replace them with _
to indicate this.
The name func
for a function doesn't tell you what it does. Names should be as descriptive as possible, in this case sin
seems sensible
def sin(x):
return math.sin(x)
Functions
The function sin
doesn't actually give you anything, as is now clearer, and I'd remove it altogether.
The calculation of you integration is discrete, and might be used separately. I would put that inside a function
def monte_carlo(n):
resf = 0
for _ in range(n):
uniform_rand_no = random.uniform(0, math.pi)
resf += math.sin(uniform_rand_no)
return math.pi * resf / 10000
Note that I've:
- Allowed you to pick the number of iterations
- Added spaces around operators
I'm not really sure what resf
means, otherwise I would have renamed that too.
Main
All your code will execute when you import your file. If you use the following you can gate execution solely to when the file is run:
if __name__ == "__main__":
for _ in range(10):
...
Everything
Putting all this together you would get:
import random
import math
def monte_carlo(n):
resf = 0
for _ in range(n):
uniform_rand_no = random.uniform(0, math.pi)
resf += math.sin(uniform_rand_no)
return math.pi * resf / 10000
if __name__ == "__main__":
integral = 0
n = 10000
for _ in range(10):
integral += math.pi * monte_carlo(n) / n
print(integral / 10)