如何使用c ++中的正态分布生成真值或假值

时间:2022-04-24 00:30:48

I want to do that without using boost and "strange" things, I want to fill a board randomly for the game of life.

我想在没有使用提升和“奇怪”的事情的情况下做到这一点,我想为生命游戏随机填充一块板。

4 个解决方案

#1


3  

Just use stlib's rand():

只需使用stlib的rand():

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

bool randomBool() {
  return rand() % 2 == 1;
}

int main ()
{
 /* initialize random seed: */
  srand ( time(NULL) );

/* fill up your game board here */

}

Just be aware that it will not yield perfectly uniform results. In order to achieve that, you'll probably have to implement something yourself. For non-cryptographic purposes, you're probably ok anyway.

请注意,它不会产生完全一致的结果。为了达到这个目的,你可能必须自己实现一些东西。对于非加密目的,无论如何你可能还可以。

#2


11  

C++11 and do it with the following

C ++ 11并使用以下内容完成

#include <random>
#include <vector>

int main()
{
    std::mt19937 gen;
    std::bernoulli_distribution     dist;

    std::vector<bool> bv;
    bv.reserve(100);

    for(unsigned i=0; i!=100; ++i)
            bv.push_back( dist(gen));

    return 0;
}

Also I don't really see what the difference between uniform and normal distribution would be for a bool.

另外,我并没有真正看到统一和正态分布之间的区别是什么。

see here for C++ random numbers. http://en.cppreference.com/w/cpp/numeric/random

在这里看到C ++随机数。 http://en.cppreference.com/w/cpp/numeric/random

#3


1  

Boost isn't strange, it isn't far off being considered as standard. (in fact much of it was the backbone of new library features in the new standard -- c++11). So I wouldn't rule it out.

提升并不奇怪,它被认为是标准的不远。 (事实上​​,其中很大一部分是新标准中新库功能的支柱--c ++ 11)。所以我不排除它。

http://www.boost.org/doc/libs/1_49_0/libs/math/doc/sf_and_dist/html/math_toolkit/dist/dist_ref/dists/normal_dist.html

Outside of a system like that, you going to have to implement it yourself which is more a mathmatics question.

在这样的系统之外,你将不得不自己实现它,这更像是一个数学问题。

#4


1  

I think you'll want to use C++11's uniform distribution. You'll need #include <random>.

我想你会想要使用C ++ 11的统一发行版。你需要#include

std::default_random_engine defaultRandomEngine(std::random_device{}());
std::uniform_int_distribution<> uniformDistribution(0, 1);
for(int i = 0; i < 100; ++i)
{
    std::cout << static_cast<bool>(uniformDistribution(defaultRandomEngine)) << std::endl;
}

#1


3  

Just use stlib's rand():

只需使用stlib的rand():

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

bool randomBool() {
  return rand() % 2 == 1;
}

int main ()
{
 /* initialize random seed: */
  srand ( time(NULL) );

/* fill up your game board here */

}

Just be aware that it will not yield perfectly uniform results. In order to achieve that, you'll probably have to implement something yourself. For non-cryptographic purposes, you're probably ok anyway.

请注意,它不会产生完全一致的结果。为了达到这个目的,你可能必须自己实现一些东西。对于非加密目的,无论如何你可能还可以。

#2


11  

C++11 and do it with the following

C ++ 11并使用以下内容完成

#include <random>
#include <vector>

int main()
{
    std::mt19937 gen;
    std::bernoulli_distribution     dist;

    std::vector<bool> bv;
    bv.reserve(100);

    for(unsigned i=0; i!=100; ++i)
            bv.push_back( dist(gen));

    return 0;
}

Also I don't really see what the difference between uniform and normal distribution would be for a bool.

另外,我并没有真正看到统一和正态分布之间的区别是什么。

see here for C++ random numbers. http://en.cppreference.com/w/cpp/numeric/random

在这里看到C ++随机数。 http://en.cppreference.com/w/cpp/numeric/random

#3


1  

Boost isn't strange, it isn't far off being considered as standard. (in fact much of it was the backbone of new library features in the new standard -- c++11). So I wouldn't rule it out.

提升并不奇怪,它被认为是标准的不远。 (事实上​​,其中很大一部分是新标准中新库功能的支柱--c ++ 11)。所以我不排除它。

http://www.boost.org/doc/libs/1_49_0/libs/math/doc/sf_and_dist/html/math_toolkit/dist/dist_ref/dists/normal_dist.html

Outside of a system like that, you going to have to implement it yourself which is more a mathmatics question.

在这样的系统之外,你将不得不自己实现它,这更像是一个数学问题。

#4


1  

I think you'll want to use C++11's uniform distribution. You'll need #include <random>.

我想你会想要使用C ++ 11的统一发行版。你需要#include

std::default_random_engine defaultRandomEngine(std::random_device{}());
std::uniform_int_distribution<> uniformDistribution(0, 1);
for(int i = 0; i < 100; ++i)
{
    std::cout << static_cast<bool>(uniformDistribution(defaultRandomEngine)) << std::endl;
}