Skip to content

Instantly share code, notes, and snippets.

@Hydrotoast
Created August 31, 2013 05:32
Show Gist options
  • Save Hydrotoast/6396391 to your computer and use it in GitHub Desktop.
Save Hydrotoast/6396391 to your computer and use it in GitHub Desktop.
import prompt
import predicate
import random
DartThrown = prompt.for_int('Enter number of darts to throw', is_legal= predicate.is_positive)
count = 0
for i in range(DartThrown):
#print('Dart Thrown')
x = random.uniform(-1, 1)
y = random.uniform(-1, 1)
#print(' x=', x,'/', 'y=', y)
if x**2+y**2 <= 1:
#print(' Inside Circle')
count += 1
#print(' Inside Count', count)
#print(' Outside Circle')
#print(' Inside Count=', count)
pi_approximate = (count/DartThrown)*4
print('pi is approximately =', pi_approximate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment