srand()函数---理解
来源:互联网 发布:数据使用保密协议 编辑:程序博客网 时间:2024/05/29 11:35
srand()函数, 原型void srand(unsigned int seed)
个人理解:srand()就是一个随机生成器。seed是表示随机生机器的种子(种子可以理解是用来初始化的,也就是起点的意思)
而常常出现的问题是,种子相同,出现的随机数也是相同的。因此为了防止出现这种情况常常使用系统时间来初始化。
原程序:试试这2种情况,就知道它们之间的区别了。
程序1:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
unsigned int seed = 10;
unsigned int i = 0;
srand(seed);
for (i = 0; i < seed; i++)
{
printf("%d ", rand()%100);
}
printf("\n");
return 0;
}
程序2:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
unsigned int i = 0;
srand(unsigned time(NULL));
for (i = 0; i < seed; i++)
{
printf("%d ", rand()%100);
}
printf("\n");
return 0;
}
- srand()函数---理解
- srand ()函数
- srand() 函数
- srand 函数
- Srand函数
- srand函数
- srand函数
- srand()函数
- srand函数
- 随机函数srand()
- srand() rand()函数
- rand()、srand()函数用法
- srand和 rand函数.
- rand()和srand()函数
- rand()和srand()函数
- 函数rand()与srand()
- srand函数资料收集
- rand()和srand()函数
- 设计模式C++之七(AbstractFactory抽象工厂模式)
- USB 2.0 OTG行動版規格概述
- 设计模式C++之八(Adapter适配器模式)
- 设计模式C++之九(Template Method模板方法模式)
- Android开机自启动程序设置及控制方法
- srand()函数---理解
- Swing布局例子(三)
- 设计模式C++之十(Builder建造者模式)
- 设计模式C++之十一(Bridge桥梁模式)
- Android的TextView/EditText使用CharacterStyle&SpannableString来处理图片显示、字体样式、超链接等
- 编写出色CSS代码的13个建议
- 设计模式C++之十二(Command命令模式)
- myeclipse快捷键
- windows下mysql免安装配置