关于随机函数rand()的使用
2017-03-31 19:03
204 查看
转自:https://my.oschina.net/jixin/blog/119433
在很多时候,程序中会用到随机数,在C++中就要用到专门用以产生随机数的标准库函数rand(),它会产生一个无符号整数,范围在0~32767,即两字节16位的整数最大值。而GNU
C++产生的随机数范围为2147483647。
范围中的每一个数在每次随机调用rand时都有相同的概率被选中。
调用时
,需要引用头文件<cstdlib>,示例代码
//掷20次筛子,每五个一行输出
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw ;
#include <cstdlib>
using std::rand;
int _tmain(int argc, _TCHAR* argv[])
{
for(int i=1;i<=20;i++)
{
cout<<setw(10)<<(1+rand()%6); //比例缩放,6称为缩放因子
if(i%5==0)
{
cout<<endl;
}
}
return 0;
}
当我们多次执行后,我们会发现每次执行的结果是一样的,既然是随机,这是为什么呢???
这是因为,rand函数不是真正的随机数生成器,而srand()会设置供rand()使用的随机数种子。如果你在第一次调用rand()之前没有调用srand(),那么系统会为你自动调用srand()。而使用同种子相同的数调用
rand()会导致相同的随机数序列被生成。如果要确保每次产生的都不一样,我们需要引用一个专门为rand设置随机化种子的函数srand().示例代码如下:
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
using std::cin ;
#include <iomanip>
using std::setw ;
#include <cstdlib>
using std::rand;
using std::srand;
int _tmain(int argc, _TCHAR* argv[])
{
unsigned int seed;
cout<<"输入随机化种子(它是一个无符号整数)";
cin>>seed;
srand(seed);
for(int i=1;i<=20;i++)
{
cout<<setw(10)<<(1+rand()%6);//比例缩放,6称为缩放因子
if(i%5==0)
{
cout<<endl;
}
}
return 0;
}
结果1种子为:67
2种子为76
3当再次执行后,种子仍然为76的时候,结果和上次执行的一样:
OK,,,,
举例:
在QUDPsocket程序的应用;
函数段:
double WeatherBalloon::temperature() const
{
return -20.0 + (2.0 * std::rand() / (RAND_MAX + 1.0));
}
返回值:-18 摄氏度~ -20摄氏度之间;
解析:rand()/(RAND_MAX + 1.0)
范围在:[0,1); 所以
2*(......)在范围[0,2);
在很多时候,程序中会用到随机数,在C++中就要用到专门用以产生随机数的标准库函数rand(),它会产生一个无符号整数,范围在0~32767,即两字节16位的整数最大值。而GNU
C++产生的随机数范围为2147483647。
范围中的每一个数在每次随机调用rand时都有相同的概率被选中。
调用时
,需要引用头文件<cstdlib>,示例代码
//掷20次筛子,每五个一行输出
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw ;
#include <cstdlib>
using std::rand;
int _tmain(int argc, _TCHAR* argv[])
{
for(int i=1;i<=20;i++)
{
cout<<setw(10)<<(1+rand()%6); //比例缩放,6称为缩放因子
if(i%5==0)
{
cout<<endl;
}
}
return 0;
}
当我们多次执行后,我们会发现每次执行的结果是一样的,既然是随机,这是为什么呢???
这是因为,rand函数不是真正的随机数生成器,而srand()会设置供rand()使用的随机数种子。如果你在第一次调用rand()之前没有调用srand(),那么系统会为你自动调用srand()。而使用同种子相同的数调用
rand()会导致相同的随机数序列被生成。如果要确保每次产生的都不一样,我们需要引用一个专门为rand设置随机化种子的函数srand().示例代码如下:
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
using std::cin ;
#include <iomanip>
using std::setw ;
#include <cstdlib>
using std::rand;
using std::srand;
int _tmain(int argc, _TCHAR* argv[])
{
unsigned int seed;
cout<<"输入随机化种子(它是一个无符号整数)";
cin>>seed;
srand(seed);
for(int i=1;i<=20;i++)
{
cout<<setw(10)<<(1+rand()%6);//比例缩放,6称为缩放因子
if(i%5==0)
{
cout<<endl;
}
}
return 0;
}
结果1种子为:67
2种子为76
3当再次执行后,种子仍然为76的时候,结果和上次执行的一样:
OK,,,,
举例:
在QUDPsocket程序的应用;
函数段:
double WeatherBalloon::temperature() const
{
return -20.0 + (2.0 * std::rand() / (RAND_MAX + 1.0));
}
返回值:-18 摄氏度~ -20摄氏度之间;
解析:rand()/(RAND_MAX + 1.0)
范围在:[0,1); 所以
2*(......)在范围[0,2);
相关文章推荐
- matlab随机函数rand使用中应注意的问题
- SQL function中使用rand随机函数
- 随机函数 rand,srand,random,srandom 的区别与使用
- C++中随机函数rand()和srand()的用法以及time()的使用
- c语言中,关于随机函数的使用详解
- 随机函数 rand,srand,random,srandom 的区别与使用
- 关于c语言的随机函数rand()
- 在js中关于随机函数的使用
- 关于rand和srand函数使用2
- 关于rand使用及随机数再现问题
- C中随机函数rand()使用时要注意的问题
- 关于随机函数srand()与rand()的实验
- srand()及rand()随机函数使用的两种方法-技术原创。
- 关于rand和srand函数使用的一点心得
- 二十四 随机函数 rand,srand,random,srandom 的区别与使用
- SQL function中使用rand随机函数
- php数组函数序列 之shuffle()和array_rand() 随机函数使用介绍
- php数组函数序列 之shuffle()和array_rand() 随机函数使用介绍
- 随机函数 rand,srand,random,srandom 的区别与使用
- 关于rand()函数srand()函数的使用(转)