rand和srand怎么用?
首先说明一下,下面所谓的随机是“伪随机”。 rand()函数的作用是产生[0,RAND_MAX]之间的一个整数,先看rand函数的用法:
#include<iostream>
using namespace std;
int main()
{int i;cout << rand() << endl;cout << rand() << endl;cout << rand() << endl; cout << RAND_MAX << endl;return 0;
}
结果为:
41
18467
6334
32767
每次运行上述程序,得到的结果都是一样的。其实随机数是这么产生的,对于一个给定的种子而言,后面的随机数序列都定了(不是真正的随机,是“伪随机”,只是人看起来像是随机数而已),而srand函数就是给定种子的函数,对于上述程序而言,默认的给定种子是1,验证如下:
#include<iostream>
using namespace std;
int main()
{srand(1);cout << rand() << endl;cout << rand() << endl;cout << rand() << endl; srand(0);cout << rand() << endl;cout << rand() << endl;cout << rand() << endl; return 0;
}
结果为:
41
18467
6334
38
7719
21238
欣赏程序:
#include<iostream>
#include<ctime>
using namespace std;
int main()
{srand(time(NULL));cout << rand() << endl;cout << rand() << endl;cout << rand() << endl; return 0;
}
结果为:
32473
26952
31764
看看下面程序,结果居然是一样的,想想为什么?
#include<iostream>
#include<ctime>
using namespace std;int getRandom()
{srand(time(NULL));return rand();
}int main()
{int i;for(i = 0; i < 10; i++){cout << getRandom() << endl;}return 0;
}
结果都是780(但每次运行的结果都不一致,也就是说不一定是780, 但是,这10个数却是相同的),之所以都是780,是因为,连续10次调用很快,系统时间来不及更换。本人实验过,当把10变成一个较大的数时,所得的结果便不一样了,因为系统时间在走动。
那么,如何让程序以某概率执行某一部分呢?可以参考如下程序:
#include<iostream>
#include<ctime>
using namespace std;int main()
{int i;int odd = 0;int even = 0;srand(time(NULL));for(i = 0; i < 10000; i++){if(1 == rand() % 2)odd++;elseeven++;}cout << odd << endl << even << endl;return 0;
}
结果为:
4976
5024
#include<iostream>
#include<ctime>
using namespace std;int main()
{int i;int a1 = 0;int a2 = 0;int a3 = 0;int n;srand(time(NULL));for(i = 0; i < 10000; i++){n = rand();if(1 == n % 3)a1++;else if(2 == n % 3)a2++;elsea3++;}cout << a1 << endl << a2 << endl << a3 << endl;return 0;
}
结果为:
3340
3348
3312
#include<iostream>
#include<ctime>
using namespace std;int main()
{int i;int a1 = 0;int a2 = 0;int a3 = 0;int a4 = 0;int n;srand(time(NULL));for(i = 0; i < 10000; i++){n = rand();if(1 == n % 4)a1++;else if(2 == n % 4)a2++;else if(3 == n % 4)a3++;elsea4++;}cout << a1 << endl << a2 << endl << a3 << endl << a4 << endl;return 0;
}
结果为:
2522
2502
2500
2476
#include<iostream>
#include<ctime>
using namespace std;int main()
{int i;int a1 = 0;int a2 = 0;int a3 = 0;int a4 = 0;int a5 = 0;int n;srand(time(NULL));for(i = 0; i < 10000; i++){n = rand();if(1 == n % 5)a1++;else if(2 == n % 5)a2++;else if(3 == n % 5)a3++;else if(4 == n % 5)a4++;elsea5++;}cout << a1 << endl << a2 << endl << a3 << endl << a4 << endl << a5 << endl;return 0;
}
结果:
1922
2034
2043
1997
2004
由此看来,对于某一种子而言,循环调用rand函数产生的随机数还真有一点伪随机性,挺好的。
rand和srand怎么用?相关推荐
- 【C++】random随机数与【C++11】/rand()和srand()的用法
文章目录 随机数 1. c++ 11 random随机数的使用(推荐使用) 1.1 1.2 1.3 1.4 2.1 C++中随机函数rand()和srand()的用法(老本版) 2.2 限制随机数的范 ...
- rand()和srand()关系很简单——一看就明白(通过一个可移植的源码)
1 函数rand和srand实现及描述 #include <stdlib.h> //供rand()使用的种子数,初值为1 unsigned long int next = 1; /* * ...
- rand和srand
rand()和srand()函数我们应该接触的比较多了,之前就遇到过,弄懂了,但是现在又忘了.再次遇见写篇文章记录一下. srand()用来设定一个随机数种子,其参数必须是整数,不同的随机数种子使ra ...
- C++中随机函数rand()和srand()的用法
一.rand() 函数名: rand 功 能: 随机数发生器 用 法: int rand(void); 所在头文件: stdlib.h 函数说明 : rand()的 ...
- C语言之rand()和srand()函数
1.rand().srand()函数介绍 srand 初始化随机种子,rand 产生随机数 定义函数:int rand(void) 函数功能:产生随机数 函数说明:rand的内部实现是用线性同余法做的 ...
- C语言随机数:rand()和srand(time(NULL))的使用
C语言随机数 rand()简介 srand()简介 使用 rand()简介 使用该函数首先应在开头包含头文件<stdlib.h> #include<stdlib.h> //(C ...
- 【C语言】随机数函数rand和srand
文章目录 一.随机数函数 1.rand().srand() 2.time() 二.案例实现 1.案例描述 2.代码实现 一.随机数函数 1.rand().srand() C语言产生随机数要用到的函数是 ...
- rand和srand的用法(转载)
首先我们要对rand&srand有个总体的看法:srand初始化随机种子,rand产生随机数,下面将详细说明. rand(产生随机数) 表头文件: #include<stdlib.h&g ...
- rand()和srand()函数的用法
一.rand() rand()函数用来产生随机数,但是,rand()的内部实现是用线性同余法实现的,是伪随机数,由于周期较长,因此在一定范围内可以看成是随机的. rand()会返回一个范围在0到RAN ...
- C 语言 rand() 和 srand() 使用方法
前言 有时我们想用 rand() 生成一组随机数,虽然我们调用了 srand(),但是生成的还是相同值.为什么会产生这种情况?又该如何解决?下面将用第一视角一起探究这其中的奥秘. 问题出现 场景描述: ...
最新文章
- 【组队学习】【26期】编程实践(Python办公自动化)
- 实用!一键生成数据库文档,堪称数据库界的Swagger
- 区块链论文研读12:谨慎日志合约 Discreet Log Contracts,详细 清晰 通俗易懂
- Atitit.跨语言 java c#.net php js常用的codec encode算法api 兼容性 应该内置到语言里面...
- yii2 跨域请求配置_伸手党系列四:vuecli3.0以上 使用 proxy 进行跨域设置
- 2017年前端最受欢迎调查报告:ES 6,React和Sublime Text依然霸主
- WPF的ComboBox 数据模板自定义
- golang 上下文 Context
- 索尼发布Xperia 8手机:采用骁龙630处理器
- linux 报错 bash ‘/bin/sh: Syntax error: “(” unexpected
- 基于php重庆旅游设计网站作品
- python gmm em算法 2维数据_AI大语音(六)——混合高斯模型(GMM)(深度解析)...
- 实习成长之路:MySQL一: 一条SQL查询语句是如何执行的?
- 从进化的角度看为啥要均贫富
- iar烧录程序步骤_如何利用IAR单片机编程软件建立烧录程序
- 古代神话体系--古希腊、罗马、北欧和中国
- Normalized Mutual information
- 男孩子学医好还是学计算机好,男孩子学医选什么专业好 最有前景的医学类专业...
- 不同windows服务器之间同步文件,WindowsServer2016配置DFS实现两个服务器之间文件同步...
- 简单日志(公开日记)