Ch12.6: Advanced Distributions
Beyond Uniform Integers
C++'s <random> library provides many distributions beyond
uniform_int_distribution. All of these work seamlessly with
ibuf_white_hole_engine because it satisfies the UniformRandomBitGenerator
requirement.
Some commonly used distributions include:
::std::uniform_real_distribution— uniform floating-point values::std::normal_distribution— normal (Gaussian) distribution::std::bernoulli_distribution— boolean values with given probability::std::binomial_distribution— number of successes in n trials::std::poisson_distribution— events in a fixed interval::std::exponential_distribution— time between events
For a complete list and detailed documentation, see cppreference — Random number generation.
Example: Normal Distribution
The normal (Gaussian) distribution is commonly used in statistics, simulations, and machine learning. It produces values clustered around a mean with a given standard deviation.
#include <fast_io.h>
#include <fast_io_device.h>
#include <random>
int main() {
using namespace ::fast_io::io;
// Create secure random engine
::fast_io::ibuf_white_hole_engine eng;
// Normal distribution: mean = 0.0, standard deviation = 1.0
::std::normal_distribution<double> dist(0.0, 1.0);
// Generate 10 values from normal distribution
println("Values from N(0, 1):");
for (::std::size_t i{}; i != 10; ++i) {
println(dist(eng));
}
// Different parameters: mean = 100.0, std dev = 15.0 (IQ scores)
::std::normal_distribution<double> iq_dist(100.0, 15.0);
println("\nSimulated IQ scores:");
for (::std::size_t i{}; i != 5; ++i) {
println(iq_dist(eng));
}
}
The distribution handles all the complexity of transforming uniform random bits into normally-distributed values using algorithms like Box-Muller or Ziggurat.
Example: Bernoulli Distribution
The Bernoulli distribution produces boolean values (true or false)
with a given probability. It's useful for simulating coin flips, yes/no decisions, or any
binary outcome.
#include <fast_io.h>
#include <fast_io_device.h>
#include <random>
int main() {
using namespace ::fast_io::io;
::fast_io::ibuf_white_hole_engine eng;
// Fair coin: 50% heads
::std::bernoulli_distribution fair_coin(0.5);
// Biased coin: 70% heads
::std::bernoulli_distribution biased_coin(0.7);
println("Fair coin flips (50% heads):");
for (::std::size_t i{}; i != 10; ++i) {
print(fair_coin(eng) ? "H" : "T", " ");
}
print("\n");
println("Biased coin flips (70% heads):");
for (::std::size_t i{}; i != 10; ++i) {
print(biased_coin(eng) ? "H" : "T", " ");
}
print("\n");
}
Using Distributions Correctly
Remember the key principles:
- Never use modulo to generate values in a range. Always use the appropriate distribution.
- Choose the right distribution for your use case — uniform, normal, Bernoulli, etc.
-
Use
ibuf_white_hole_enginefor secure, automatic seeding. -
For reproducible results, seed
::std::mt19937_64with::std::seed_seqfrom the secure engine.
Further Reading
For complete documentation on all available distributions, their parameters, and usage examples, see:
Key Takeaways
-
C++ provides many distributions beyond
uniform_int_distribution, includingnormal_distribution,bernoulli_distribution, and many others. -
All distributions work seamlessly with
ibuf_white_hole_engine. - Choose the appropriate distribution for your use case — don't manually transform uniform values.
- For complete documentation, see cppreference.