秒及毫秒的随机数种子

引入头文件:

#include "sys/timeb.h"

随机数种子(毫秒):

srandom(time(NULL));
struct timeb timeSeed;
ftime(&timeSeed);
srand(timeSeed.time * 1000 + timeSeed.millitm);  // milli time

随机数种子(秒):

unsigned int tseed = time(NULL);
srand(tseed);

使用随机数:

int iRand = rand()%NUM;            //产生0-NUM的随机数iRand

你可能感兴趣的:(游戏开发)