在c++中使用rand()函数的正确方法是什么?

时间:2021-04-04 21:33:18

I'm doing a book exercise that says to write a program that generates psuedorandom numbers. I started off simple with.

我正在做一本书练习,说要编写一个生成psuedorandom数字的程序。我从简单开始。

#include "std_lib_facilities.h"

int randint()
{
    int random = 0;
    random = rand();
    return random;
}

int main()
{
    char input = 0;
    cout << "Press any character and enter to generate a random number." << endl;
    while (cin >> input)
    cout << randint() << endl;
    keep_window_open();
}

I noticed that each time the program was run, there would be the same "random" output. So I looked into random number generators and decided to try seeding by including this first in randint().

我注意到每次运行程序时,都会有相同的“随机”输出。因此,我研究了随机数生成器,并决定尝试在randint()中加入第一个。

    srand(5355);

Which just generated the same number over and over (I feel stupid now for implementing it.)

它一遍又一遍地生成相同的数字(我觉得实现它很愚蠢)。

So I thought I'd be clever and implement the seed like this.

所以我想我应该很聪明地实现这个种子。

srand(rand());

This basically just did the same as the program did in the first place but outputted a different set of numbers (which makes sense since the first number generated by rand() is always 41.)

这基本上与程序一开始所做的一样,但是输出了一组不同的数字(这是有意义的,因为rand()生成的第一个数字总是41)。

The only thing I could think of to make this more random is to:

我能想到的唯一能让这个更随机的是:

  1. Have the user input a number and set that as the seed (which would be easy to implement, but this is a last resort) OR
  2. 让用户输入一个数字并将其设置为种子(这很容易实现,但这是最后一招)还是
  3. Somehow have the seed be set to the computer clock or some other constantly changing number.
  4. 以某种方式将种子设置为计算机时钟或其他不断变化的数字。

Am I in over my head and should I stop now? Is option 2 difficult to implement? Any other ideas?

我是不是在我的脑海中,我应该停下来吗?选项2很难实现吗?任何其他想法?

Thanks in advance.

提前谢谢。

5 个解决方案

#1


27  

Option 2 isn't difficult, here you go:

选项2不难,给你:

srand(time(NULL));

you'll need to include stdlib.h for srand() and time.h for time().

您需要包含stdlib。h表示srand()和time。h时间()。

#2


8  

srand() should only be used once:

srand()只能使用一次:

int randint()
{
    int random = rand();
    return random;
}

int main()
{
    // To get a unique sequence the random number generator should only be
    // seeded once during the life of the application.
    // As long as you don't try and start the application mulitple times a second
    // you can use time() to get a ever changing seed point that only repeats every
    // 60 or so years (assuming 32 bit clock).
    srand(time(NULL));
    // Comment the above line out if you need to debug with deterministic behavior.

    char input = 0;
    cout << "Press any character and enter to generate a random number." << endl;

    while (cin >> input)
    {
        cout << randint() << endl;
    }
    keep_window_open();
}

#3


6  

It is common to seed the random number generator with the current time. Try:

将随机数生成器与当前时间进行混合是很常见的。试一试:

srand(time(NULL));

将srand(时间(NULL));

#4


4  

The problem is that if you don't seed the generator it will seed itself with 0 (as if srand(0) were called). PRNGs are designed to generate the same sequence when seeded the same (due to the fact that PNRGs are not really random, they're deterministic algorithms and maybe a bit because it's quite useful for testing).

问题是,如果您不给生成器添加种子,它将会以0(好像srand(0)被调用)来播种。PRNGs被设计成在播种相同的时候产生相同的序列(由于PNRGs不是真正的随机,它们是确定性的算法,可能还有一点因为它对测试非常有用)。

When you're trying to seed it with a random number using

当你用一个随机数来播种的时候

srand(rand());

you're in effect doing:

你实际上做的事情:

srand(0);
x = rand();   // x will always be the same.
srand(x);

As FigBug mentioned, using the time to seed the generator is commonly used.

正如FigBug提到的,通常使用时间来种子生成器。

#5


0  

I think that the point of these articles is to have a go at implementing the algorithm that is in rand() not how to seed it effectively.

我认为这些文章的重点是尝试实现rand()中的算法,而不是如何有效地进行种子。

producing (pseudo) random numbers is non trivial and is worth investigating different techniques of generating them. I don't think that simply using rand() is what the authors had in mind.

生成(伪)随机数是非平凡的,值得研究生成随机数的不同技术。我不认为仅仅使用rand()是作者的想法。

#1


27  

Option 2 isn't difficult, here you go:

选项2不难,给你:

srand(time(NULL));

you'll need to include stdlib.h for srand() and time.h for time().

您需要包含stdlib。h表示srand()和time。h时间()。

#2


8  

srand() should only be used once:

srand()只能使用一次:

int randint()
{
    int random = rand();
    return random;
}

int main()
{
    // To get a unique sequence the random number generator should only be
    // seeded once during the life of the application.
    // As long as you don't try and start the application mulitple times a second
    // you can use time() to get a ever changing seed point that only repeats every
    // 60 or so years (assuming 32 bit clock).
    srand(time(NULL));
    // Comment the above line out if you need to debug with deterministic behavior.

    char input = 0;
    cout << "Press any character and enter to generate a random number." << endl;

    while (cin >> input)
    {
        cout << randint() << endl;
    }
    keep_window_open();
}

#3


6  

It is common to seed the random number generator with the current time. Try:

将随机数生成器与当前时间进行混合是很常见的。试一试:

srand(time(NULL));

将srand(时间(NULL));

#4


4  

The problem is that if you don't seed the generator it will seed itself with 0 (as if srand(0) were called). PRNGs are designed to generate the same sequence when seeded the same (due to the fact that PNRGs are not really random, they're deterministic algorithms and maybe a bit because it's quite useful for testing).

问题是,如果您不给生成器添加种子,它将会以0(好像srand(0)被调用)来播种。PRNGs被设计成在播种相同的时候产生相同的序列(由于PNRGs不是真正的随机,它们是确定性的算法,可能还有一点因为它对测试非常有用)。

When you're trying to seed it with a random number using

当你用一个随机数来播种的时候

srand(rand());

you're in effect doing:

你实际上做的事情:

srand(0);
x = rand();   // x will always be the same.
srand(x);

As FigBug mentioned, using the time to seed the generator is commonly used.

正如FigBug提到的,通常使用时间来种子生成器。

#5


0  

I think that the point of these articles is to have a go at implementing the algorithm that is in rand() not how to seed it effectively.

我认为这些文章的重点是尝试实现rand()中的算法,而不是如何有效地进行种子。

producing (pseudo) random numbers is non trivial and is worth investigating different techniques of generating them. I don't think that simply using rand() is what the authors had in mind.

生成(伪)随机数是非平凡的,值得研究生成随机数的不同技术。我不认为仅仅使用rand()是作者的想法。