当我在 Visual C++ 中给出 sleep(1000) 以使 srand() 工作时出错

发布于 2024-10-16 15:58:08 字数 484 浏览 5 评论 0原文

我有以下程序:

srand((unsigned) time(NULL));
for (int w = 0; w < 10; w++) {
    int ran_x;
    ran_x = rand() % 255;
    cout << "nRandom X = " << ran_x << endl;
    //some more lines of code
    Sleep(1000);
}

我在 Visual C++ 2008 上运行它,当我运行该程序时,它没有显示任何错误或警告。但是当我运行它时,有时它运行良好,有时它会在中间停止并给出此错误“此应用程序已请求运行时以不寻常的方式终止它。 请联系应用程序的支持团队以获取更多信息。”

我该怎么办?是否可以在不使用 Sleep() 函数的情况下完成此操作,并且仍然获得随机生成的值。因为如果我删除 Sleep(1000),它不会给出任何错误但它也不给出随机值

i have following program:

srand((unsigned) time(NULL));
for (int w = 0; w < 10; w++) {
    int ran_x;
    ran_x = rand() % 255;
    cout << "nRandom X = " << ran_x << endl;
    //some more lines of code
    Sleep(1000);
}

I am running it on visual c++ 2008, When I run this program, it doesnt show any errors or warnings. But when I run it, some of the times it runs fine, and some of the times it stops in the middle and gives this error "This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information."

What shall I do? Is it possible to do it with out using Sleep() function and still get randomly generated values. Because if I remove Sleep(1000), it doesnt give any error but it doesnt gives random values either

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

毁梦 2024-10-23 15:58:08

显然你不应该睡觉。在我看来,代码看起来很正常,只要您只调用 srand() 一次。如果您在秒内多次调用整个代码块,则 time(NULL) 将返回相同的秒值,并且 srand() 将以相同的数字开始生成伪随机数,并选择相同的 10 个后续数字集数字....

Obviously you shouldn't have to sleep. Code looks sane to me, as long as you only call srand() once. If you call this entire block of code multiple times intra-second, then time(NULL) will be returning the same second value and srand() will start the pseudo-random number generation at the same number, selecting the same set of 10 subsequent numbers....

安静被遗忘 2024-10-23 15:58:08

与 gcc 一起工作没有任何问题

#include <iostream>
#include <cstdlib>

int main (int argc, char *argv[])
{    
    srand( time(0) );

    for (int w = 0; w < 10; w++)
    {
        int ran_x = rand() % 255;

        std::cout<<"\nRandom X = " << ran_x << std::endl;

        sleep(1);
    }

    return 0;
}

Works without any problems with gcc

#include <iostream>
#include <cstdlib>

int main (int argc, char *argv[])
{    
    srand( time(0) );

    for (int w = 0; w < 10; w++)
    {
        int ran_x = rand() % 255;

        std::cout<<"\nRandom X = " << ran_x << std::endl;

        sleep(1);
    }

    return 0;
}
谁把谁当真 2024-10-23 15:58:08

在我看来,你的程序应该在没有睡眠呼叫的情况下完美运行。事实上,我在 VS2008 上似乎完美地工作了。我相信您的问题一定是在您认为不相关而删除的代码中。

Seems to me your program should work perfectly without the sleep call. In fact seems to work for me on VS2008 perfectly. I believe your problems must be in code that you have removed thinking it irrelevant.

情仇皆在手 2024-10-23 15:58:08

您发布的代码片段几乎不会导致您的应用程序终止,无论 Sleep 与否。

因为如果我删除 Sleep(1000),它
没有给出任何错误,但它没有
也给出随机值。

好吧,rand() 肯定会为您提供伪随机数,尽管 PRNG 实现可能不会返回沿返回值的位均匀分布的随机值,即在许多实现中,较高位变化更多通常比低位高,这就是为什么您的代码对于选择 0 到 255 之间的随机值来说是一个糟糕的选择。

一般来说,我建议从标准库的 rand/srand 切换到像 boost 的 mersenne twiner 这样的实现(boost::random),或者至少参见

http://c-faq.com/lib/randrange.html

The code snippet you posted is hardly responsible for your application terminating, Sleep or not.

Because if I remove Sleep(1000), it
doesnt give any error but it doesnt
gives random values either.

Well, rand() certainly gives you pseudo-random numbers, although the PRNG implementation might not return random values evenly distributed along the bits of the returned value, i.e. in many implementations, the higher bits are changing more often than the lower bits, which is why your code is a poor choice for selecting a random value between 0 and 255.

In general, I'd recommend switching from your standard library's rand/srand to an implementation like boost's mersenne twister (boost::random), or at least see

http://c-faq.com/lib/randrange.html

云仙小弟 2024-10-23 15:58:08

“再加几行代码”的内容是什么?

<心理调试>我敢打赌,您的代码直接或间接取决于您之前生成的随机值。这段代码可能是一个除法,或者涉及设置某个容器的长度,并且当生成的随机数为 0 时会停止。

What's the content of "some more lines of code"?

<psychic debugging>I bet you have code that there that, directly or indirectly, depends on the random value you generated earlier. This code will likely be a division, or involve setting the length of some container, and borks when the generated random number is 0.</psychic debugging>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文