每日一JavaScript:组成三角形的概率

一根木棒随机折成三段,它们能组成一个三角形的概率是多少?

求解过程就是使用JavaScript不断模拟将一根木棒随机折成三段,看它们是否能组成三角形,最后用能组成三角形的次数除以总的次数,得到最终的结果。模拟的次数越多,得到的结果就越精确。
(1)如何模拟木棒的折断。木棒的长度固定,可以假设其为1,而折成3段只需两个断点,因此执行两次Math.random()来得到断点的位置,这样就将木棒随机分为了三段,可以很容易获得每一段的长度。而模拟的次数则很容易通过for循环进行控制。
(2)如何判断是否能组成三角形。三角形的一个重要性质就是“两边之和大于第三边”。所以只要较短的两段大于第三段即可。在这个例子中,只要有一段的长度超过0.5就说明不能组成三角形。


<script type="text/javascript">
<!--
var totalCount=10000;
var okCount=0;
for(var i=0;i<totalCount;i++) {
var duan1=Math.random();              //得到第一个断点
var duan2=Math.random();              //得到第二个断点
var l1=Math.min(duan1,duan2);         //得到第一段长度
var l2=Math.max(duan1,duan2)-l1;      //得到第二段长度
var l3=1-l1-l2;                       //得到第三段长度
if(l1>=0.5 || l2>=0.5 || l3>=0.5)
continue;                             //只要有一边长超过0.5,说明不能组成三角形
okCount++;
}
alert(okCount/totalCount);
//-->
</script>


37 Responses to “每日一JavaScript:组成三角形的概率”

  1. xiaorsz说道:

    额?运行了下怎么没有结果?

  2. 沙渺说道:

    有一件事,就是我们在折木棒的时候,一般都会受到直觉和习惯的影响,出现一些极端划分的概率比较低。

    比如说给我根长度为100的木棒,就很容易折出类似25/35/40这样的,却很难折出1/1/98。

    换句话说就是各种剖分方式的权一定是不等的,“完全随机”这个命题意义不大。

  3. 胡戈戈说道:

    @xiaorsz 都是因为找不到一个好的显示代码的插件,现在应该可以了
    @沙渺 你说的有道理,其实他这个只是在模拟任意三个边能组成三角形的概率到底有多少,而它这个是机器来完成的,相对准确性不会受到人的因素影响

  4. Yacca说道:

    hgg童鞋竟然开始研究js…

  5. 洒水车说道:

    什么乱八七糟的

  6. 专用汽车说道:

    数学不好,看不懂

  7. bigCat说道:

    – – 居然是数学强淫,偶只喜欢折腾DOM

  8. … [Trackback]

    […] Read More on on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  9. Cam Girls说道:

    … [Trackback]

    […] Find More on on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  10. … [Trackback]

    […] Info on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  11. cach choi bingo说道:

    … [Trackback]

    […] Find More on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  12. scarpe gif说道:

    … [Trackback]

    […] Info to that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  13. smoke shop说道:

    … [Trackback]

    […] Find More on on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  14. Michael Lutz说道:

    … [Trackback]

    […] Read More to that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  15. … [Trackback]

    […] Read More Info here on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  16. … [Trackback]

    […] Read More to that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  17. dang nhap 12bet说道:

    … [Trackback]

    […] Read More on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  18. link m88asia说道:

    … [Trackback]

    […] Find More Information here on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  19. … [Trackback]

    […] Information on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  20. asikqq pro说道:

    … [Trackback]

    […] Info on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  21. … [Trackback]

    […] Read More here to that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  22. … [Trackback]

    […] Find More to that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  23. vape juice说道:

    … [Trackback]

    […] Info to that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  24. THC cartridges说道:

    … [Trackback]

    […] Here you will find 37758 more Info on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  25. vape oil说道:

    … [Trackback]

    […] Information to that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  26. … [Trackback]

    […] There you can find 44456 additional Info on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  27. … [Trackback]

    […] Find More Information here on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  28. kandypens说道:

    … [Trackback]

    […] Read More Info here on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  29. THC vape juice说道:

    … [Trackback]

    […] Info to that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  30. … [Trackback]

    […] Read More on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  31. … [Trackback]

    […] Info to that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  32. Free Porn说道:

    … [Trackback]

    […] Find More Info here to that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  33. Porn说道:

    … [Trackback]

    […] Information to that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  34. … [Trackback]

    […] There you will find 49508 more Info on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  35. … [Trackback]

    […] Read More Information here on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  36. … [Trackback]

    […] Find More to that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

  37. … [Trackback]

    […] Find More here on that Topic: hugege.com/2008/11/26/js-sanjiao/ […]

Post a Comment

Blue Captcha Image
Refresh

*