以给定的概率获取true或false [英] Get true or false with a given probability
问题描述
我试图在c ++中写一个函数,它将基于给定的概率返回true或false。所以,例如,如果给定的概率是0.634,那么63.4%的时间函数将返回true。我试过几个不同的东西,失败了。任何帮助?
I'm trying to write a function in c++ that will return true or false based on a probability given. So, for example if the probability given was 0.634 then, 63.4% of the time the function would return true. I've tried a few different things, and failed. Any help?
推荐答案
如果你想在C ++ 11中做这个,你可以使用它的各种随机数引擎, uniform_real_distribution
提供了一个很好的结果。以下代码演示:
If you'd like to do this in C++11, you can use its various random number engines, combined with the uniform_real_distribution
to provide a good result. The following code demonstrates:
#include <random>
std::knuth_b rand_engine; // replace knuth_b with one of the engines listed below
std::uniform_real_distribution<> uniform_zero_to_one(0.0, 1.0);
bool random_bool_with_prob( double prob ) // probability between 0.0 and 1.0
{
return uniform_zero_to_one(rand_engine) >= prob;
}
或者,您可以使用 bernoulli_distribution
,它直接给你一个具有指定概率的 bool
。它所需的概率是返回true的概率,因此它正是你需要的:
Alternately, you can use the bernoulli_distribution
, which directly gives you a bool
with the specified probability. The probability it takes is the probability of returning true, so it is exactly what you need:
#include <random>
std::knuth_b rand_engine; // replace knuth_b with one of the engines listed below
bool random_bool_with_prob( double prob ) // probability between 0.0 and 1.0
{
std::bernoulli_distribution d(prob);
return d(rand_engine);
}
如果你的概率是固定的,那么你可以把它移出函数所以:
If your probability is fixed, then you can move it out of the function like so:
#include <random>
std::knuth_b rand_engine; // replace knuth_b with one of the engines listed below
std::bernoulli_distribution random_bool_generator( prob ); // replace "prob" with your probability
bool random_bool()
{
return random_bool_generator( rand_engine );
}
或者如果你想让鸽友静静,你可以将它们绑定在一起: / p>
Or if you want to get fancier still, you can bind them together:
#include <random>
#include <functional>
std::knuth_b rand_engine; // replace knuth_b with one of the engines listed below
std::bernoulli_distribution random_bool_generator( prob ); // replace "prob" with your probability
auto random_bool = std::bind( random_bool_generator, rand_engine )
// Now call random_bool() to get your random boolean with the specified probability.
您可以将 knuth_b
标准引擎:
-
std :: linear_congruential_engine
-
std :: mersenne_twister_engine
-
std :: subtract_with_carry_engine
li>
std::linear_congruential_engine
std::mersenne_twister_engine
std::subtract_with_carry_engine
或更多,这是上述的版本,参数化的各种方式。我的参考文献列出了以下内容:
or many more, which are versions of the above, parameterized various ways. My reference lists the following:
-
std :: default_random_engine
-
std :: minstd_rand0
-
std :: minstd_rand
-
-
std :: mt19937
> std :: mt19337_64 -
std :: ranlux24_base
-
std :: ranlux48_base
-
std :: ranlux24
-
std :: ranlux48
-
std :: knuth_b
std::default_random_engine
(Implementation defined.)std::minstd_rand0
std::minstd_rand
std::mt19937
std::mt19337_64
std::ranlux24_base
std::ranlux48_base
std::ranlux24
std::ranlux48
std::knuth_b
如果这还不够,还有一些标准适配器可以进一步扰乱随机数序列:
And if that isn't enough, there are some standard adaptors that can further perturb the random number sequence:
-
std :: discard_block_engine
它会每次丢弃指定数量的生成值来调整引擎 -
std :: independent_bits_engine
,它使引擎能够产生具有指定位数的随机值。 (对您的特殊需求不重要。) -
std :: shuffle_order_engine
通过排列其生成值的顺序来适应引擎。
std::discard_block_engine
which adapts an engine by discarding a given number of generated values each time.std::independent_bits_engine
which adapts an engine to produce random values with a specified number of bits. (Not important to your particular need.)std::shuffle_order_engine
which adapts an engine by permutation of the order of their generated values.
第二个列表中的生成器派生自第一个列表中的基本生成器,具有特定参数,适配器或两者。例如, knuth_b
等效于 shuffle_order_engine< linear_congruential_engine< uint32_t,16807,0,2147483647>,256>
,根据我的参考书。
The generators in the second list are derived from the base generators in the first list, either with specific parameters, adaptors or both. For example, knuth_b
is equivalent to shuffle_order_engine< linear_congruential_engine< uint32_t, 16807, 0, 2147483647>, 256>
, according to my reference book. (The C++ Standard Library, Second Edition, by Nicolai Josuttis, a great reference work.)
您可以在网上找到更多资讯,包括这篇文章。简介: http://en.wikipedia.org/wiki/C++11 #Extensible_random_number_facility
You can find more information online, including this brief introduction here: http://en.wikipedia.org/wiki/C++11#Extensible_random_number_facility
这里还有更多文档: http://en.cppreference.com/w/cpp/numeric/random
您可能需要修改 rand_engine
上面提供种子。上面的示例使用默认种子。如果你想要不同的种子,请参见cppreference.com如何种子。
You will probably want to modify the declaration of rand_engine
above to provide a seed. The example above uses the default seed. See cppreference.com for how to seed it if you want a different seed.
这篇关于以给定的概率获取true或false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!