Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
16
Task/Monte-Carlo-methods/Python/monte-carlo-methods-1.py
Normal file
16
Task/Monte-Carlo-methods/Python/monte-carlo-methods-1.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
>>> import random, math
|
||||
>>> throws = 1000
|
||||
>>> 4.0 * sum(math.hypot(*[random.random()*2-1
|
||||
for q in [0,1]]) < 1
|
||||
for p in xrange(throws)) / float(throws)
|
||||
3.1520000000000001
|
||||
>>> throws = 1000000
|
||||
>>> 4.0 * sum(math.hypot(*[random.random()*2-1
|
||||
for q in [0,1]]) < 1
|
||||
for p in xrange(throws)) / float(throws)
|
||||
3.1396359999999999
|
||||
>>> throws = 100000000
|
||||
>>> 4.0 * sum(math.hypot(*[random.random()*2-1
|
||||
for q in [0,1]]) < 1
|
||||
for p in xrange(throws)) / float(throws)
|
||||
3.1415666400000002
|
||||
17
Task/Monte-Carlo-methods/Python/monte-carlo-methods-2.py
Normal file
17
Task/Monte-Carlo-methods/Python/monte-carlo-methods-2.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
from random import random
|
||||
from math import hypot
|
||||
try:
|
||||
import psyco
|
||||
psyco.full()
|
||||
except:
|
||||
pass
|
||||
|
||||
def pi(nthrows):
|
||||
inside = 0
|
||||
for i in xrange(nthrows):
|
||||
if hypot(random(), random()) < 1:
|
||||
inside += 1
|
||||
return 4.0 * inside / nthrows
|
||||
|
||||
for n in [10**4, 10**6, 10**7, 10**8]:
|
||||
print "%9d: %07f" % (n, pi(n))
|
||||
4
Task/Monte-Carlo-methods/Python/monte-carlo-methods-3.py
Normal file
4
Task/Monte-Carlo-methods/Python/monte-carlo-methods-3.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import numpy as np
|
||||
|
||||
n = input('Number of samples: ')
|
||||
print np.sum(np.random.rand(n)**2+np.random.rand(n)**2<1)/float(n)*4
|
||||
Loading…
Add table
Add a link
Reference in a new issue