我正在尝试用c ++编写一个函数,它将根据给定的概率返回true或false.因此,例如,如果给定的概率是0.634,那么63.4%的函数将返回true.我尝试了一些不同的东西,但都失败了.有帮助吗?
如果你想在C++ 11中这样做,你可以使用它的各种随机数引擎,结合使用uniform_real_distribution
来提供一个好的结果.以下代码演示:
#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的概率,所以它正是你需要的:
#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); }
如果您的概率是固定的,那么您可以将其移出函数,如下所示:
#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 ); }
或者如果你想要更加漂亮,你可以将它们绑在一起:
#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
或更多,以上的版本,参数化各种方式.我的参考列出了以下内容:
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::discard_block_engine
它通过每次丢弃给定数量的生成值来适应引擎.
std::independent_bits_engine
它使引擎适应产生具有指定位数的随机值.(对您的特殊需求并不重要.)
std::shuffle_order_engine
它通过排列其生成值的顺序来适应引擎.
第二个列表中的生成器派生自第一个列表中的基本生成器,具有特定参数,适配器或两者.例如,根据我的参考书,knuth_b
相当于shuffle_order_engine< linear_congruential_engine< uint32_t, 16807, 0, 2147483647>, 256>
.(C++标准库,第二版,Nicolai Josuttis,一篇很棒的参考书.)
您可以在线查找更多信息,包括此简介:http: //en.wikipedia.org/wiki/C++11#Extensible_random_number_facility
这里有更多文档:http: //en.cppreference.com/w/cpp/numeric/random
您可能希望修改rand_engine
上面的声明以提供种子.上面的示例使用默认种子.如果您想要一个不同的种子,请参阅cppreference.com了解如何播种它.