简体   繁体   English

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

[英]What's the Right Way to use the rand() Function in C++?

I'm doing a book exercise that says to write a program that generates psuedorandom numbers. 我正在做一本书练习,说要写一个生成伪随机数的程序。 I started off simple with. 我从简单开始。

#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. Somehow have the seed be set to the computer clock or some other constantly changing number. 将种子以某种方式设置为计算机时钟或其他不断变化的数字。

Am I in over my head and should I stop now? 我在头上,现在应该停下来吗? Is option 2 difficult to implement? 选项2难以实施吗? Any other ideas? 还有其他想法吗?

Thanks in advance. 提前致谢。

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.hsrand()time.htime()

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

It is common to seed the random number generator with the current time. 通常用当前时间播种随机数生成器。 Try: 尝试:

srand(time(NULL)); srand(time(NULL));

The problem is that if you don't seed the generator it will seed itself with 0 (as if srand(0) were called). 问题是,如果不对生成器进行种子设置,则它将自身以0种子设置(就像srand(0)一样)。 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). PRNG被设计为在播种相同时生成相同的序列(由于PNRG并不是真正随机的,因此它们是确定性算法,也许有点,因为它对于测试非常有用)。

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所提到的 ,通常使用时间为生成器添加种子。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM