在Visual C++中怎样获取随机数

Microsoft Visual C++5.0 在Visual C++中怎样获取随机数?不知如何获取指定范围内的随机数?(即实现Turbo C中random函数的功能)

使用rand函数获得随机数。rand函数返回的随机数在0-RAND_MAX(32767)之间。
<wbr><wbr><wbr><wbr>例子:<br><wbr><wbr><wbr><wbr><span style="font-family:Courier New; font-size:13px; word-wrap:normal; word-break:normal; line-height:19px"><span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px">#include &lt;stdlib.h&gt;</span><span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span><span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px">#include &lt;stdio.h&gt;</span><span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span><span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px">#include &lt;time.h&gt;</span><span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>void</strong></span>main(<span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>void</strong></span>)<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span>{<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span><span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>int</strong></span>i;<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span>srand( (<span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>unsigned</strong></span>)time( NULL ) );<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span><span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>for</strong></span>( i = 0; i &lt; 10;i++ )<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span>printf( " %6d\n", rand() );<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span>}<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></span><br><wbr><wbr><wbr><wbr>在调用这个函数前,最好先调用srand函数,如srand( (unsigned)time( NULL ) ),这样可以每次产生的随机数<span style="word-wrap:normal; word-break:normal; line-height:21px; color:rgb(0,0,0)">序列不同。</span><br><wbr><wbr><wbr><wbr>如果要实现类似0-1之间的函数,可以如下:<br><wbr><wbr><wbr><wbr><span style="font-family:Courier New; font-size:13px; word-wrap:normal; word-break:normal; line-height:19px"><span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>double</strong></span>randf()<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span>{<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span><span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>return</strong></span>(<span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>double</strong></span>)(rand()/(<span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>double</strong></span>)RAND_MAX);<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span>}<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></span><br><wbr><wbr><wbr><wbr>如果要实现类似Turbo C的random函数,可以如下:<br><wbr><wbr><wbr><wbr><span style="font-family:Courier New; font-size:13px; word-wrap:normal; word-break:normal; line-height:19px"><span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>int</strong></span>random(<span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>int</strong></span>number)<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span>{<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span><span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>return</strong></span>(<span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>int</strong></span>)(number/(<span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>float</strong></span>)RAND_MAX * rand());<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span>}</span><br><br><br><span style="word-wrap:normal; word-break:normal; line-height:19px">问题: 怎样获得一个真正的随机数?要知道,rand()是不能产生真正的随机数的!即使不能产生真正的随机数,也要大概接近呀!而rand()好象每次的随机都一样。</span> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> <span style="word-wrap:normal; word-break:normal; line-height:21px; font-weight:bold"><br></span></p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> <span style="color:#660000; word-wrap:normal; word-break:normal; line-height:19px"><strong>回答</strong>:</span></p> <p style="margin-top:0px; margin-right:0px; margin-bottom:5px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; list-style-type:none; list-style-position:initial; word-wrap:normal; word-break:normal; line-height:21px"> <span style="word-wrap:normal; word-break:normal; line-height:19px"><wbr><wbr><wbr><wbr>之所以rand()每次的随机数都一样是因为rand()函数使用不正确。各种编程语言返回的随机数(确切地说是伪随机数)实际上都是根据递推公式计算的 一组数值,当序列足够长,这组数值近似满足均匀分布。如果计算伪随机序列的初始数值(称为种子)相同,则计算出来的伪随机序列就是完全相同的。这个特性被 有的软件利用于加密和解密。加密时,可以用某个种子数生成一个伪随机序列并对数据进行处理;解密时,再利用种子数生成一个伪随机序列并对加密数据进行还 原。这样,对于不知道种子数的人要想解密就需要多费些事了。当然,这种完全相同的序列对于你来说是非常糟糕的。要解决这个问题,需要在每次产生随机序列 前,先指定不同的种子,这样计算出来的随机序列就不会完全相同了。你可以在调用rand()函数之前调用srand( (unsigned)time( NULL ) ),这样以time函数值(即当前时间)作为种子数,因为两次调用rand函数的时间通常是不同的,这样就可以保证随机性了。你也可以使用srand函数 来人为指定种子数。Windows 9x/NT的游戏FreeCell就允许用户指定种子数,这样用户如果一次游戏没有成功,下次还可以以同样的发牌结果再玩一次。<br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><br><wbr>我按照上述方法并不能产生随机数,仅产生公差为3或4的等差数列:<br><wbr><wbr><wbr><wbr>#include &lt;stdlib.h&gt;<br><wbr><wbr><wbr><wbr>#include &lt;iostream.h&gt;<br><wbr><wbr><wbr><wbr>#include &lt;conio.h&gt;<br><wbr><wbr><wbr><wbr>#include &lt;time.h&gt;<br><wbr><wbr><wbr><wbr>void main()<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>for(int i=0;i&lt;100000;i++)<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>srand( (unsigned)time( NULL ) );<br><wbr><wbr><wbr><wbr>cout&lt;&lt;rand()&lt;&lt;endl;<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><strong>答:</strong>你的程序是有问题的,你每产生一个随机数之前,都调用一次srand,而由于计算机运行很快,所以你每次用time得 到的时间都是一样的(time的时间精度较低,只有55ms)。这样相当于使用同一个种子产生随机序列,所以产生的随机数总是相同的。你应该把srand 放在循环外:<br><wbr><wbr><wbr><wbr><span style="font-family:Courier New; font-size:13px; word-wrap:normal; word-break:normal; line-height:19px">srand( (<span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>unsigned</strong></span>)time( NULL ) );<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span><span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>for</strong></span>(<span style="color:#0000FF; word-wrap:normal; word-break:normal; line-height:19px"><strong>int</strong></span>i=0;i&lt;100000;i++)<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span>{<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span>cout &lt;&lt; rand() &lt;&lt; endl;<span style="color:#FFFFFF; word-wrap:normal; word-break:normal; line-height:19px"><br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span>}</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(获取随机数)