Created
October 6, 2017 05:54
-
-
Save thmosqueiro/3b5ca8a4447c0220d426e1bc225b6671 to your computer and use it in GitHub Desktop.
Probability of forming a triangle
This file contains hidden or 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 as np | |
Nsamples = 10000 | |
p_estimate = 0. | |
s_estimate = 0. | |
for j in range(Nsamples): | |
ps = np.random.random(2) | |
x = min(ps) | |
y = max(ps) | |
if ( ( x <= 0.5 ) and ( y >= 0.5 ) and ( y - x <= 0.5 ) ): | |
p_estimate += 1. | |
p_estimate /= Nsamples | |
print( 'P(triangle) = %5.4f' % p_estimate ) | |
print( 'Relative error = %5.4f' % ( np.abs( p_estimate - 0.25 )/0.25 ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment