Advertisement
Guest User

Finding the value of Pi using Monte Carlo techniqies

a guest
Feb 12th, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.31 KB | None | 0 0
  1. #! /usr/bin/python
  2.  
  3. import sys
  4. import math
  5. import random
  6.  
  7. print "The value of pi is:";
  8.  
  9. num=int(sys.argv[1]);
  10. radius=1.0;
  11. inside=0;
  12.  
  13. i=0;
  14. while i<num:
  15.     i=i+1;
  16.     x = random.random();
  17.     y = random.random();
  18.     dist = math.sqrt(x*x + y*y);
  19.     if dist<radius:
  20.         inside=inside+1;
  21.  
  22. pi = 4.0*inside/num;
  23. print pi;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement