首页 新闻 搜索 专区 学院

随机返回值的概率和预想的不一样呢?

0
[已关闭问题]
从1到100循环,如果返回0则输出Heads,否则输出Tails.<br>根据概率应该输出Heads和Tails的概率基本一样啊,应该都差不多为50次才对,可是结果是却令我非常吃惊。居然是Heads 26次,Tails 74次。<br>这是为什么呢?<br><div class="cnblogs_code"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">&nbsp;1</span><img src="/Images/OutliningIndicators/None.gif" align="top"><span style="color: rgb(0, 0, 0);">#include</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 128, 128);">&nbsp;2</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">#include</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">stdlib.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 128, 128);">&nbsp;3</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"><br></span><span style="color: rgb(0, 128, 128);">&nbsp;4</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;flip();<br></span><span style="color: rgb(0, 128, 128);">&nbsp;5</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"><br></span><span style="color: rgb(0, 128, 128);">&nbsp;6</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;main()<br></span><span style="color: rgb(0, 128, 128);">&nbsp;7</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_62_297_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_62_297_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_62_297_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_62_297_Closed_Text').style.display='inline';" align="top"><img src="/Images/OutliningIndicators/ContractedBlock.gif" id="Codehighlighter1_62_297_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_62_297_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_62_297_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_62_297_Open_Text').style.display='inline';" align="top"></span><span id="Codehighlighter1_62_297_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;"><img src="/images/dot.gif"></span><span id="Codehighlighter1_62_297_Open_Text"><span style="color: rgb(0, 0, 0);">{<br></span><span style="color: rgb(0, 128, 128);">&nbsp;8</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i;<br></span><span style="color: rgb(0, 128, 128);">&nbsp;9</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;sum&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">;<br></span><span style="color: rgb(0, 128, 128);">10</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_115_215_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_115_215_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_115_215_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_115_215_Closed_Text').style.display='inline';" align="top"><img src="/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_115_215_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_115_215_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_115_215_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_115_215_Open_Text').style.display='inline';" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;(&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">100</span><span style="color: rgb(0, 0, 0);">;&nbsp;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">&nbsp;)</span><span id="Codehighlighter1_115_215_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;"><img src="/images/dot.gif"></span><span id="Codehighlighter1_115_215_Open_Text"><span style="color: rgb(0, 0, 0);">{<br></span><span style="color: rgb(0, 128, 128);">11</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_132_180_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_132_180_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_132_180_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_132_180_Closed_Text').style.display='inline';" align="top"><img src="/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_132_180_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_132_180_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_132_180_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_132_180_Open_Text').style.display='inline';" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(&nbsp;flip()&nbsp;)</span><span id="Codehighlighter1_132_180_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;"><img src="/images/dot.gif"></span><span id="Codehighlighter1_132_180_Open_Text"><span style="color: rgb(0, 0, 0);">{<br></span><span style="color: rgb(0, 128, 128);">12</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">&nbsp;flip();<br></span><span style="color: rgb(0, 128, 128);">13</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Heads\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;);<br></span><span style="color: rgb(0, 128, 128);">14</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 128, 128);">15</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 128, 128);">16</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Tails\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;);<br></span><span style="color: rgb(0, 128, 128);">17</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 128, 128);">18</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Sum&nbsp;Heads:&nbsp;%d\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;sum&nbsp;);<br></span><span style="color: rgb(0, 128, 128);">19</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;printf(&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Sum&nbsp;Tails:&nbsp;%d\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;sum&nbsp;);<br></span><span style="color: rgb(0, 128, 128);">20</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top">}</span></span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 128, 128);">21</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"><br></span><span style="color: rgb(0, 128, 128);">22</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_309_332_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_309_332_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_309_332_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_309_332_Closed_Text').style.display='inline';" align="top"><img src="/Images/OutliningIndicators/ContractedBlock.gif" id="Codehighlighter1_309_332_Closed_Image" style="display: none;" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_309_332_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_309_332_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_309_332_Open_Text').style.display='inline';" align="top"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;flip()</span><span id="Codehighlighter1_309_332_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;"><img src="/images/dot.gif"></span><span id="Codehighlighter1_309_332_Open_Text"><span style="color: rgb(0, 0, 0);">{<br></span><span style="color: rgb(0, 128, 128);">23</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/InBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;rand()&nbsp;</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">2</span><span style="color: rgb(0, 0, 0);">;<br></span><span style="color: rgb(0, 128, 128);">24</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top">}</span></span></div><br><br>
iPeterRex的主页 iPeterRex | 初学一级 | 园豆:190
提问于:2008-07-16 23:30
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册