关于C++中产生随机数

记得,当程序高度依赖随机数实现算法的时候(比如蒙特卡洛搜索,模拟退火),要产生以毫秒为单位的随机数种子,要不然完全在浪费生命……

关于如何产生随机数种子:

#include <time.h>
...
int seed=time(NULL);
srand(seed);
int m=rand()%RAND_MAX;

关于如何产生以毫秒为单位的随机数种子:

#include <time.h>
...
struct timeb timeSeed;
ftime(&timeSeed)
srand(timeSeed.time*1000+timeSeed.millitm);
int m=rand()%RAND_MAX

其中RAND_MAX是你自己预先定义的一个数字,这里只是用它表示方便。

猜你喜欢

转载自blog.csdn.net/weixin_44288817/article/details/88531890