Skip to content

Instantly share code, notes, and snippets.

@thmosqueiro
Created October 6, 2017 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmosqueiro/3b5ca8a4447c0220d426e1bc225b6671 to your computer and use it in GitHub Desktop.
Save thmosqueiro/3b5ca8a4447c0220d426e1bc225b6671 to your computer and use it in GitHub Desktop.
Probability of forming a triangle
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