
随机数字常用于许多不同类型的程序中,以下是一些示例:计算机游戏通常要使用随机数字来模拟一些随机过程,例如掷骰子或发牌。模拟程序使用随机数字来决定后续将要发生的一系列 *** 作或人与动物等的行为。可以创建使用随机数字的公式以确定特定事件在程序中出现的时间。数据分析程序可能会使用随机数字随机选择要检验的数据。计算机安全系统使用随机数字来加密敏感数据。
C++ 库有一个名为 rand() 的函数,每次调用该函数都将返回一个非负整数。要使用 rand() 函数,必须在程序中包含 <cstdlib> 头文件。以下是其用法示例:
randomNum = rand();
但是,该函数返回的数字其实是伪随机数。这意味着它们具有随机数的表现和属性,但实际上并不是随机的,它们实际上是用算法生成的。该算法需要一个起始值,称为种子,以生成数字。如果没有给出一个种子,那么它将在每次运行时产生相同的数字流。下面的程序说明了这一点:
//This program demonstrates what happens in C++ if you// try to generate random numbers without setting a "seed".#include <iostream>#include <cstdlib>// header file needed to use randusing namespace std;int main(){ // Generate and printthree random numbers cout << rand() << " "; cout << rand() << " "; cout << rand() << endl ; return 0;}第1次运行输出结果:
41 18467 : 6334
第2次运行输出结果:
41 18467 6334
在 rand 被调用之前,srand 函数要先被调用,并且 srand 在整个程序中仅被调用一次。
// This program demonstrates using random numbers when a// "seed" is provIDed for the random number generator.#include <iostream>#include <cstdlib> // header file needed to use srand and randusing namespace std;int main(){ unsigned seed; // Random generator seed // Get a nseed" value from the user cout << "Enter a seed value: "; cin >> seed; // Set the random generator seed before calling rand() srand(seed); //Now generate and print three random numbers cout << rand() << " "; cout << rand() << " "; cout << rand() << endl; return 0;}第1次运行结果:
Enter a seed value: 19
100 15331 - 209
第2次运行结果:
Enter a seed value: 171
597 10689 28587
程序的第 12 行中,使用 cin 从用户的输入获取随机数生成器种子的值。实际上,获取种子值的另一个常见做法是调用 time 函数,它是 C++ 标准库的一部分。
time 函数返回从 1970 年 1 月 1 日午夜开始到现在逝去的秒数,因此每次运行程序时,它都将提供不同的种子值。下面程序演示了 time 函数的用法。请注意,在调用它时必须给它传递一个参数 0。同时程序中包含一个新的头文件 ctime,此头文件是使用 time 函数所必需的。
//This program demonstrates using the C++ time function//to provIDe a nseed,T for the random number generator.#include <iostream>#include <cstdlib> // header file needed to use srand and rand#include <ctime> // header file needed to use timeusing namespace std;int main(){ unsigned seed; // Random generator seed // Use the time function to get a "seed” value for srand seed = time(0); srand(seed); // Now generate and print three random numbers cout << rand() << " " ; cout << rand() << " " ; cout << rand() << endl; return 0;}程序输出结果:2961 21716 181
限制随机数的范围有时程序需要一个特定范围内的随机数。要将随机数的范围限制在 1 和某个最大值 max 之间的整数,可以使用以下公式:number = rand() % max + 1;
例如,要生成 1〜6 的随机数来代表骰子的点数,则可以使用以下语句:dice = rand() % 6 + 1;
这里简单介绍一下其工作原理。求余数运算符(%)可以获得整除之后的余数。当使用通过 rand 函数返回的正整数除以6时,余数将是 0〜5 的数字。因为目标是 1〜6 的数字,所以只需要给余数加 1 即可。这个想法可以扩展到任意范围内的随机数,其通用公式如下:
number = (rand()%(maxValue - minValue +1)) + minValue;
在上述公式中,minValue 是范围内的最小值,而 maxValue 则是范围内的最大值。例如,要获得 10〜18 的随机数,可以使用以下代码给变量 number 赋值:const int MIN_VALUE = 10;
const int MAX_VALUE = 18;
number = rand() % (MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE;
总结
以上是内存溢出为你收集整理的C++随机数(rand和srand)函数用法详解全部内容,希望文章能够帮你解决C++随机数(rand和srand)函数用法详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)