赶潮流 發表於 2025-12-15 09:43:32

C语言rand函数的应用实例(随机数的生成)

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>一、rand函数</li><ul class="second_class_ul"><li>1.简介</li><li>2.返回值</li></ul><li>二、srand函数</li><ul class="second_class_ul"></ul><li>三、time函数</li><ul class="second_class_ul"><li>1.简介</li><li>2.注意</li><li>3.演示</li></ul><li>四、随机数的生成</li><ul class="second_class_ul"></ul><li>五、应用</li><ul class="second_class_ul"></ul><li>六、结语</li><ul class="second_class_ul"></ul></ul></div><p class="maodian"></p><h2>一、rand函数</h2>
<p class="maodian"></p><p class="maodian"></p><h3>1.简介</h3>
<p>用于生成随机数的函数</p>
<p>使⽤需要包含⼀个头⽂件:&lt;stdlib.h&gt;</p>
<p class="maodian"></p><h3>2.返回值</h3>
<p>返回一个介于和之间的伪随机整数。</p>
<p><strong>一个介于0和RAND_MAX之间的整数值。</strong></p>
<blockquote><p>(RAND_MAX为32767)</p></blockquote>
<p>那为什么说是伪随机数呢?</p>
<p>如下代码:</p>
<div class="jb51code"><pre class="brush:cpp;">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int main()
{
        printf("%d\n", rand());
        printf("%d\n", rand());
        printf("%d\n", rand());
        printf("%d\n", rand());
        printf("%d\n", rand());
        printf("%d\n", rand());
        printf("%d\n", rand());
        printf("%d\n", rand());
        printf("%d\n", rand());
        printf("%d\n", rand());

        return 0;
}
</pre></div>
<p>运行结果:</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509393328.png" /></p>
<p>大家可以自行尝试下,<strong>代码两次运行结果一模一样</strong></p>
<p><strong>这也说明了rand生成的数是伪随机数</strong></p>
<p>那该如何生成真正的随机数呢?</p>
<p><strong>rand函数是对⼀个叫种⼦的基准值进⾏运算⽣成的随机数</strong>。之所以前⾯每次运⾏程序产⽣的随机数序列是⼀样的,是因为rand函数⽣成随机数的默认种⼦是1。而要想得到真正的随机数就要使种子不断变化。<strong>这就可以用到srand函数</strong>。</p>
<p class="maodian"></p><h2>二、srand函数</h2>
<p>函数原型:</p>
<div class="jb51code"><pre class="brush:cpp;">void srand(unsigned int seed);
</pre></div>
<p>srand函数的使⽤需要包含⼀个头⽂件:&lt;stdlib.h&gt;</p>
<p>调⽤rand函数之前先调⽤srand函数,</p>
<p><strong>通过srand函数的参数seed来设置rand函数所需要不断变化的种子,</strong></p>
<p>使得rand函数生成的是真正的随机数。</p>
<p>如下代码:</p>
<div class="jb51code"><pre class="brush:cpp;">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int main()
{
        srand(1);
        //修改种子
        printf("%d\n", rand());
        printf("%d\n", rand());
        printf("%d\n", rand());
        printf("%d\n", rand());

        return 0;
}</pre></div>
<p>种子不同时运行结果:</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509393373.png" /></p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509393378.png" /></p>
<p>可以看见两次结果不同,可以见得:</p>
<p><strong>种子不一样时,生成的随机数也不同</strong></p>
<p><strong>但是,⽣成随机数的时候又需要另⼀个随机数,这就矛盾了。</strong></p>
<p class="maodian"></p><h2>三、time函数</h2>
<h3>1.简介</h3>
<p>函数说明:</p>
<p>time函数可以<strong>获取当前的系统时间</strong>,返回的是一个<strong>time_t类型</strong>的从1970年1⽉1⽇0时0分0秒到现在程序运⾏时间之间的差值,如果我们给time函数传一个空指针(NULL),就会只返回这个时间的差值,这个差值也被称为<strong>时间戳。</strong></p>
<p>返回值:</p>
<p>成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于error中。</p>
<p>头文件:</p>
<p>time函数在使用前需要包含<strong>头文件&lt; time.h &gt;</strong></p>
<p class="maodian"></p><h3>2.注意</h3>
<p><strong>由上文可知</strong>:</p>
<p>由于srand函数的参数是需要一个<strong>unsigned int 类型</strong>的值,虽然time函数的返回值为<strong>time_t类型</strong>的,但是时间戳是一个正数;所以我们只要把time函数的返回值<strong>强制类型转换为unsigned int类型</strong>即可。</p>
<p class="maodian"></p><h3>3.演示</h3>
<p>演示:</p>
<div class="jb51code"><pre class="brush:cpp;">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;

int main()
{
        srand((unsigned int)(time(NULL)));
        //真正的随机数
        printf("%d\n", rand());
        printf("%d\n", rand());
        printf("%d\n", rand());
        printf("%d\n", rand());

        return 0;
}</pre></div>
<p>大家也可以自行去试一试,每次运行的结果都不相同</p>
<p>两次运行结果:</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509393369.png" /></p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509393395.png" /></p>
<p class="maodian"></p><h2>四、随机数的生成</h2>
<p>现在,我们可以生成出一个<strong>介于0和RAND_MAX之间的整数值</strong></p>
<blockquote><p>(RAND_MAX为32767)</p></blockquote>
<p>但是要想生成 <strong>0到100</strong> 或 <strong>100到200</strong> 等自定义随机数要怎么办呢?</p>
<p>这里需要用到一个运算符(%),这个运算符是<strong>取模运算符</strong>,得到的就是除以一个数的余数。<strong>我们只需要将得到的随机数除上规定的范围那个数就可以得到需要范围的数。</strong></p>
<p>例如:</p>
<ul><li><p>要生成 <strong>0到99</strong> 随机数:<br /><strong>就写rand ()%100;</strong></p></li><li><p>要生成 <strong>1到100</strong> 随机数:<br /><strong>就写rand ()%100+1;</strong></p></li><li><p>要生成 <strong>100到200</strong> 随机数:<br /><strong>就写rand ()%101+100;</strong></p></li></ul>
<p>万能公式:<br />生成 a 到 b 随机数:<br /><strong>a + rand( ) % ( b - a + 1 );</strong></p>
<p>演示:</p>
<div class="jb51code"><pre class="brush:cpp;">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;

int main()
{
        srand((unsigned int)(time(NULL)));
        //真正的随机数
        printf("%d\n", rand() % 100);
        //0-99
        printf("%d\n", rand() % 100 + 1);
        //1-100
        printf("%d\n", rand() % 101 + 100);
        //100-200

        return 0;
}</pre></div>
<p>运行结果:</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509393363.png" /></p>
<p class="maodian"></p><h2>五、应用</h2>
<p>学会了随机数的生成,我们可以应用到随机数完成一些游戏</p>
<p><strong>扫雷中布置雷是随机的</strong>,故可以用到rand函数以及本文的内容</p>
<p>我们就可以完成一个扫雷游戏</p>
<p>下一期我们详解扫雷!!!</p>
<p class="maodian"></p><h2>六、结语</h2>
<p>本期资料来自于:</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509393331.png" /></p>
<p>https://legacy.cplusplus.com/</p>
<p>到此这篇关于C语言rand函数应用(随机数的生成)的文章就介绍到这了,更多相关C语言rand函数应用内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>C语言中随机数rand()函数详解</li><li>详解C语言中rand函数的使用</li><li>C语言的随机数rand()函数详解</li><li>C语言使用rand函数生成随机数</li><li>C语言rand和srand函数使用方法介绍</li><li>C语言srand函数的详细用法示例小结</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: C语言rand函数的应用实例(随机数的生成)