Created
February 10, 2014 22:53
-
-
Save gkthiruvathukal/8925871 to your computer and use it in GitHub Desktop.
Simple MC in Python and NumPy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import numpy | |
>>> a = numpy.random.random(1000000) | |
>>> n = 1000000 | |
>>> a = numpy.random.random(n) | |
>>> b = numpy.random.random(n) | |
>>> a | |
array([ 0.13849378, 0.72228591, 0.49816557, ..., 0.08655529, | |
0.02044737, 0.86337785]) | |
>>> b | |
array([ 0.28050175, 0.05473387, 0.99651725, ..., 0.37804476, | |
0.71068628, 0.11741604]) | |
>>> result = a*a + b*b | |
>>> dartsInCircle = result[result <= 1.0] | |
>>> len(dartsInCircle) | |
785444 | |
>>> n | |
1000000 | |
>>> 4.0 * len(dartsInCircle)/n | |
3.141776 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment