Skip to content

Commit 63f08cd

Browse files
committed
Example: Monte carlo estimation of pi using arrayfire
1 parent 6aacf08 commit 63f08cd

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

examples/monte_carlo_pi.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/python
2+
from arrayfire import (array, randu)
3+
import arrayfire as af
4+
import random
5+
6+
def calc_pi_device(samples):
7+
x = randu(samples)
8+
y = randu(samples)
9+
return 4 * af.sum((x * x + y * y) < 1) / samples
10+
11+
if __name__ == "__main__":
12+
samples=1000000
13+
print("Monte carlo estimate of pi with %d samples: %f" % (samples, calc_pi_device(samples)))

0 commit comments

Comments
 (0)