如何每次在同一程序/函数中生成不同的随机数字集?

发布于 2024-09-13 16:50:19 字数 1110 浏览 2 评论 0原文

据我了解,使用 srand(time(0)) 有助于设置随机种子。但是,以下代码为两个不同的列表存储同一组数字。

想知道当以下函数被多次调用时如何生成不同的数字集。

void storeRandomNos(std::list<int>& dataToStore)
{

    int noofElements = 0;
    srand(time(0));


    noofElements = (rand() % 14 ) + 1;

    while ( noofElements --)
    {
        dataToStore.push_back(rand() % 20 + 1 );
    }
}

这是其余的代码。

void printList(const std::list<int>& dataElements, const char* msg);
void storeRandomNos(std::list<int>& dataToStore);
int main()
{
    std::list<int> numberColl1;
    std::list<int> numberColl2;


    storeRandomNos(numberColl1);
    storeRandomNos(numberColl2);

    printList(numberColl1 , "List1");
    printList(numberColl2 , "Second list");


}


void printList(const std::list<int>& dataElements, const char* msg)
{

    std::cout << msg << std::endl;
    std::list<int>::const_iterator curLoc = dataElements.begin();

    for ( ; curLoc != dataElements.end() ; ++curLoc)
    {
        std::cout << *curLoc << ' ';
    }
}

I understand, using srand(time(0)), helps in setting the random seed. However, the following code, stores the same set of numbers for two different lists.

Wondering, how do I generate the different set of numbers when the following function gets called more than once.

void storeRandomNos(std::list<int>& dataToStore)
{

    int noofElements = 0;
    srand(time(0));


    noofElements = (rand() % 14 ) + 1;

    while ( noofElements --)
    {
        dataToStore.push_back(rand() % 20 + 1 );
    }
}

Here is the rest of the code.

void printList(const std::list<int>& dataElements, const char* msg);
void storeRandomNos(std::list<int>& dataToStore);
int main()
{
    std::list<int> numberColl1;
    std::list<int> numberColl2;


    storeRandomNos(numberColl1);
    storeRandomNos(numberColl2);

    printList(numberColl1 , "List1");
    printList(numberColl2 , "Second list");


}


void printList(const std::list<int>& dataElements, const char* msg)
{

    std::cout << msg << std::endl;
    std::list<int>::const_iterator curLoc = dataElements.begin();

    for ( ; curLoc != dataElements.end() ; ++curLoc)
    {
        std::cout << *curLoc << ' ';
    }
}

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

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

发布评论

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

评论(4

人间不值得 2024-09-20 16:50:21

在程序开始时执行一次srand(time(0))

do the srand(time(0)) once at the beginning of your program.

拍不死你 2024-09-20 16:50:21

您需要在每个调用rand()的线程中使用srand(time(0))仅一次您的程序获取伪随机数。

You need to use srand(time(0)) only once in every thread that calls rand() in your program to get pseudo random numbers.

醉殇 2024-09-20 16:50:20

仅在主程序启动时初始化 RNG 一次。不是每次你进入你的职能时。否则,该函数很可能会在同一秒内被调用两次,这可能会为 time(0) 提供相同的结果。

Initialize the RNG only once, when the main program starts. Not each time you step into your function. Otherwise, chances are the function is called twice within the same second, which may give you identical results for time(0).

不离久伴 2024-09-20 16:50:19

伪随机生成器(例如 rand())只是一个数学函数,它接受输入(种子)并对其进行一些处理。它返回它产生的新值,并将其设置为新种子。下次它将使用新的种子值。

因为计算机是确定性的,所以每次使用相同的种子调用 rand() 时,它都会产生相同的输出值。这就是为什么它是随机的。

在您的示例中,您使用了相同的种子两次,因为 time(0) 返回以秒为单位的时间,并且您的两个函数调用发生在同一秒内(因为计算机非常快)。

正如其他评论者所说,只需将种子设置为相当随机的值(即当前时间)一次。

A pseudo-random generator such as rand(), is simply a mathematical function that takes an input -- the seed -- and does some processing on it. It returns the new value it has produced and also sets that as the new seed. The next time it will use that new seed value.

Because computers are deterministic, every time you call rand() with the same seed, it will produce the same output value. That is why it is pseudo random.

In your example, you used the same seed twice because time(0) returns the time in seconds and your two function calls took place within that same second (because computers are pretty fast).

As the other commenters have said, it's only necessary to seed to a fairly random value (i.e. the current time) once.

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