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?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
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:Alternately, you can use the
bernoulli_distribution
, which directly gives you abool
with the specified probability. The probability it takes is the probability of returning true, so it is exactly what you need:If your probability is fixed, then you can move it out of the function like so:
Or if you want to get fancier still, you can bind them together:
You can replace
knuth_b
with any of the standard engines: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
(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
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.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 toshuffle_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.)You can find more information online, including this brief introduction here: http://en.wikipedia.org/wiki/C++11#Extensible_random_number_facility
There's more documentation here: http://en.cppreference.com/w/cpp/numeric/random
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.Logic:
rand()
returns a random number between0
andRAND_MAX
(including both), with equal probability for each number. So by dividing the result byRAND_MAX
we get a random number between0
and1
. This allows us to choose a area of - in your example 63.4% of this segment, e.g. from0
to0.634
- and check if the result fell in that area.Now comes the tricky part: we don't want to get both
0
and1
! Why? Because we want probability0
to never be true, that's why we need the<p
(rather than the<=p
) - so that whenp=0
you'll never get true.However, if you can also have
1
as the result, then in the case wherep=1
there is a very small chance you get false!That's why instead of dividing by
MAX_RAND
you divide byMAX_RAND+1.0
. Also note that I added1.0
instead of1
to turn the number into a double (otherwise I might get an overflow ifMAX_RAND==INT_MAX
)Finally, here's an alternate implementation without the division: