Skip to content

Instantly share code, notes, and snippets.

@gkthiruvathukal
Created February 10, 2014 22:53
Show Gist options
  • Save gkthiruvathukal/8925871 to your computer and use it in GitHub Desktop.
Save gkthiruvathukal/8925871 to your computer and use it in GitHub Desktop.
Simple MC in Python and NumPy
>>> 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