from __future__ import division import random #initialize triangle counter triangle=0 #total number of iterations total=10000 for x in range(1,total): #pick two places to slice seg1=random.random() seg2=random.random() #determine lengths of 3 resulting segments a,b,c if seg1>seg2: a=seg2 b=seg1-seg2 c=1-seg1 else: a=seg1 b=seg2-seg1 c=1-seg2 #if triangle is possible, increment triangle counter if a<b+c and b<a+c and c<a+b: triangle=triangle+1 print(triangle/total)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question