C++ 随机布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8422709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:32:58  来源:igfitidea点击:

Random Boolean Value

c++randomctime

提问by Rich Byden

I'm trying to generate a random int that is either 0 or 1 in C++. Right now, I receive a 0 every time I run this code, and I'm not sure why. What's the problem here?

我正在尝试生成一个在 C++ 中为 0 或 1 的随机整数。现在,每次运行此代码时我都会收到 0,但我不知道为什么。这里有什么问题?

#include <ctime>
#include <cstdlib>

srand(time(0));
int randomval = rand() % 2;
cout << randomval << endl;

回答by Michael Krelin - hacker

It is called bad luck. Try it again.

它被称为厄运。再试一次。

回答by cgossain

I know this is an older question but I believe this answers the question properly.

我知道这是一个较旧的问题,但我相信这正确地回答了这个问题。

Don't re-seed the the generator every time you run that code.

每次运行该代码时,不要重新为生成器设置种子。

By seeding it to the same value every time, you're just gonna get the same "random" number. Remember this is a Pseudo-Random number generator, so based on the seed value, a "random" number will be generated. So if you seed it with the same number every time you're just gonna get the same number every time.

通过每次将其播种到相同的值,您将获得相同的“随机”数。请记住,这是一个伪随机数生成器,因此会根据种子值生成一个“随机”数。所以如果你每次都用相同的数字播种,你每次都会得到相同的数字。

The solution is to call srand(time(NULL)) only once in your program execution. Then, each call to rand() will give you a different number every time.

解决方案是在程序执行中只调用一次 srand(time(NULL)) 。然后,每次调用 rand() 都会给你一个不同的数字。

回答by Kiril Kirov

On theory, there's 50% chance you get 0, and 50 - 1. You may want to try with different modulo - for example 100, to check if this works. And I'm sure it does.

理论上,您有 50% 的机会获得0,而 50 - 1。您可能想尝试使用不同的模数 - 例如 100,以检查这是否有效。而且我确定确实如此。

You have just ran this code a few times, not enough.

您只运行了几次此代码,还不够。

Other idea to test it:

测试它的其他想法:

srand(time(0));
for( int i = 0; i < 1000000; ++i )
{
    assert( 0 == ( rand() % 2 ) );
}

回答by Bazyl Ichabod Horsey

bool random() {
    if (rand() % 2 == 0)
        return true;
    else return false;
}

回答by toftis

I would like to add that when you use srand(time(0));the "random number" will always be the same in the same second. When I tried to run your program 10000 times and group it by uniq I saw that the number would not change within a second.

我想补充一点,当你使用srand(time(0));“随机数”时,在同一秒内总是相同的。当我尝试运行您的程序 10000 次并按 uniq 对其进行分组时,我发现该数字在一秒钟内不会改变。

for i in `seq 1 10000`; do ./a.out; done | uniq -c
    693 0
   3415 1
    675 0
    673 1
    665 0
    674 1
    668 0
    711 1
    694 0
    673 1
    459 0

回答by Azy

Call srand(time(NULL));just once.

srand(time(NULL));只打一次电话。

Then use a loop like this, you will always get a 0 or 1 this way.

然后使用这样的循环,您将始终以这种方式获得 0 或 1。

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

srand(time(NULL));

for (i=0;i<10;i++)
{
    printf("%d\n",rand() % 2);
    i++;
}

return 0;

回答by v010dya

Although your code suggests that you want to receive them equally likely, you didn't state that, and perhaps you have simply thought that it was impossible to do otherwise. If you want a different distribution, and you are willing to rewrite your code (and make it C++11 compliant), you can do the following:

尽管您的代码表明您希望以同样的可能性接收它们,但您并没有说明这一点,也许您只是认为除此之外是不可能的。如果您想要不同的发行版,并且愿意重写您的代码(并使其符合 C++11),您可以执行以下操作:

    const double chance = 0.3; // this is the chance of getting true, between 0 and 1;
    std::random_device rd;
    std::mt19937 mt(rd());
    std::bernoulli_distribution dist(chance);
    bool result = dist(mt);

If you will need to do that in a loop, only repeat the last statement dist(mt), keep all the generated objects as they are without recreating them.

如果您需要在循环中执行此操作,只需重复最后一条语句dist(mt),将所有生成的对象保持原样,而无需重新创建它们。

回答by DJ LW

You are not checking against anything. Use:

你没有检查任何东西。用:

#include <ctime>
#include <cstdlib>

srand(time(0));
int randomval = rand() % 2 == 0;
cout << randomval << endl;