首页 > 解决方案 > Srand 正在输出一个巨大的数字列表,而不仅仅是一个

问题描述

我正在使用 C++,而且我很新,但我最近遇到的麻烦之一是使用 srand。Srand 正在输出一个庞大的数字列表,直到它移动到下一个。这是我的代码,所以你可以理解我在说什么

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>

using namespace std;

void function2()
{
    int num;
    string none;
    string end1;

    while (num != 69420)
    {

        srand((unsigned) time( NULL));

        num = rand() % 100000 + 1;

        cout << num << "\n";
    }
    if (num != 69420)
    {
        cout << "gg's";
        cin >> end1;
    }
}
int main()
{
    int num;
    string none;

    while (num != 69420)
    {

        srand((unsigned) time( NULL));

        num = rand() % 100000 + 1;

        cout << "Press enter.";
        getline(cin, none);
        if (none == " ")
        {

        }
        else
        {
            function2();
        }
        if (num != 69420)
        {
            break;
        }
        else
        {
            continue;
        }
    }
}

这可能是不对的,这就是我在这里的原因。感谢任何人,如果你试图帮助我!

标签: c++srand

解决方案


推荐阅读