### Boost.Random: Basic Random Number Generation Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/random/index This example demonstrates the basic usage of the Boost.Random library for generating random numbers. It initializes a Mersenne Twister engine and a uniform integer distribution to simulate rolling a six-sided die. Dependencies include the Boost.Random library. ```cpp boost::random::mt19937 rng; // produces randomness out of thin air // see pseudo-random number generators boost::random::uniform_int_distribution<> six(1,6); // distribution that maps to 1..6 // see random number distributions int x = six(rng); // simulate rolling a die ``` -------------------------------- ### Demonstrate Uniform Integer Distribution (C++) Source: https://www.boost.org/doc/libs/latest/libs/random/example/random_demo This function demonstrates generating random integers within a specified range using Boost's uniform integer distribution and variate generator. It generates 10 samples of dice rolls (1-6). Ensure Boost.Random is linked. ```C++ #include #include #include #include #include typedef boost::minstd_rand base_generator_type; void experiment(base_generator_type & generator) { typedef boost::uniform_int<> distribution_type; typedef boost::variate_generator gen_type; gen_type die_gen(generator, distribution_type(1, 6)); boost::generator_iterator die(&die_gen); for(int i = 0; i < 10; i++) std::cout << *die++ << " "; std::cout << '\n'; } ``` -------------------------------- ### Degenerate Uniform Integer Distribution (C++) Source: https://www.boost.org/doc/libs/latest/libs/random/example/random_demo Demonstrates creating a degenerate uniform integer distribution where the minimum and maximum values are the same. This results in the generator always producing that specific value. Requires Boost.Random. ```C++ #include #include #include #include typedef boost::minstd_rand base_generator_type; int main() { base_generator_type generator(42); boost::uniform_int<> degen_dist(4,4); boost::variate_generator > deg(generator, degen_dist); std::cout << deg() << " " << deg() << " " << deg() << std::endl; return 0; } ``` -------------------------------- ### Demonstrate Uniform Real Distribution (C++) Source: https://www.boost.org/doc/libs/latest/libs/random/example/random_demo This code snippet shows how to generate random double-precision floating-point numbers between 0.0 (inclusive) and 1.0 (exclusive) using Boost's uniform real distribution and variate generator. It prints 10 such numbers. Requires Boost.Random. ```C++ #include #include #include #include typedef boost::minstd_rand base_generator_type; int main() { base_generator_type generator(42); std::cout << "10 samples of a uniform distribution in [0..1):\n"; boost::uniform_real<> uni_dist(0,1); boost::variate_generator > uni(generator, uni_dist); std::cout.setf(std::ios::fixed); for(int i = 0; i < 10; i++) std::cout << uni() << '\n'; // ... rest of main return 0; } ``` -------------------------------- ### Save and Restore Random Generator State (C++) Source: https://www.boost.org/doc/libs/latest/libs/random/example/random_demo This C++ snippet shows how to save the state of a Boost random number generator to a file and potentially restore it later. It uses `std::ofstream` and the stream insertion operator (`<<`) for saving. Requires Boost.Random and fstream. ```C++ #include #include typedef boost::minstd_rand base_generator_type; int main() { base_generator_type generator; // ... generate numbers ... { std::ofstream file("rng.saved", std::ofstream::trunc); file << generator; } // To restore, you would typically use std::ifstream and operator>> return 0; } ``` -------------------------------- ### Seed Random Generator with Time (C++) Source: https://www.boost.org/doc/libs/latest/libs/random/example/random_demo This C++ code demonstrates seeding a Boost random number generator using the current time. It casts the result of `std::time(0)` to an unsigned integer for seeding. Note the caveat about `std::time(0)` not being a robust seed for rapid calls. ```C++ #include #include typedef boost::minstd_rand base_generator_type; int main() { base_generator_type generator; // ... other code ... generator.seed(static_cast(std::time(0))); // ... rest of main return 0; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.