获取多个随机数的问题 [英] Problem with getting multiple random numbers

查看:36
本文介绍了获取多个随机数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
推荐的srand初始化方式?

我正在从 AVI 中提取帧.我希望用户选择他是想从用户给定的范围内获取所有帧,还是获取所有可用帧,或者获取用户给定的随机帧数.前两个函数工作得很好.但是对于随机帧,我总是只得到一帧,而不是给定的帧数.所以,这里用户设置帧数:

I am extracting frames from AVI. I want the user to choose wether he wants to get all frames from user given range or get all frames available or get user given number of random frames. First two functions work just fine. But with random frames I always get only one frame, not the given number of frames. So, here user sets the number of frames:

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() 中,我这样做:

then in ExtractAVIFrames() after all inits and such I do this:

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()

which calls 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 返回的数字与前一次调用中的相同.怎么了?谢谢

so, this should call GetRandomInt() function frQuantity-times and I should have frQuantity BMPs, right? But I dont, I always get just one (random one). It seems that after each call the GetRandomInt returns the same number as in previous call. What's wrong? Thank you

推荐答案

您正在使用相同的种子初始化 pseudorandom 数字生成器.只初始化一次,并使用半随机数(例如,系统时钟中的毫秒数).

You are initializing the pseudorandom number generator with the same seed. Initialize it just once, and with a semi-random (v.g., milliseconds in the system clock) number.

这篇关于获取多个随机数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆