<分区>
Possible Duplicate:
Recommended way to initialize srand?
我正在从 AVI 中提取帧。我希望用户选择他是想获取用户给定范围内的所有帧还是获取所有可用帧或获取用户给定数量的随机帧。前两个功能工作得很好。但是对于随机帧,我总是只得到一帧,而不是给定的帧数。所以,这里用户设置帧数:
case AVIINF_BUTTON_GETRAND:
extractmode=-1;
GetDlgItemText(aviinfhwnd, AVIINF_EDIT_GETRAND, charfrQuantity, 20);
frQuantity = atoi(charfrQuantity);
ExtractAVIFrames(extractmode, frFrom, frTo, frQuantity);
EndDialog(aviinfhwnd, IDCANCEL);
break;
然后在 ExtractAVIFrames() 中执行所有初始化等操作之后:
case -1://get x random frames
for (int i=1; i<=frQuantity; i++)
{
index= GetRandomInt(iNumFrames);
BYTE* pDIB = (BYTE*) AVIStreamGetFrame(pFrame, index);
CreateFromPackedDIBPointer(pDIB, index);
}
break;
调用 GetRandomInt()
int GetRandomInt(int randNumScale)
{
srand((unsigned)time(0));
int random_integer;
int range=randNumScale;
random_integer = (rand()%range)+1;
return random_integer;
}
所以,这应该调用 GetRandomInt() 函数 frQuantity-times 我应该有 frQuantity BMP,对吧?但我没有,我总是只得到一个(随机的一个)。似乎在每次调用 GetRandomInt 后返回与上一次调用相同的数字。 怎么了? 谢谢