|
7 | 7 |
|
8 | 8 | // chapter03_22-001_mersenne_twister_19937.cpp |
9 | 9 |
|
| 10 | +// See also: https://godbolt.org/z/azW71ahfK |
| 11 | + |
10 | 12 | #include <cstdint> |
11 | | -#include <ctime> |
12 | 13 | #include <iomanip> |
13 | 14 | #include <iostream> |
14 | 15 | #include <random> |
| 16 | +#include <sstream> |
15 | 17 |
|
| 18 | +template<typename EngineType> |
16 | 19 | auto do_something() -> void |
17 | 20 | { |
18 | | - using local_result_type = typename std::random_device::result_type; |
| 21 | + using local_result_type = typename EngineType::result_type; |
| 22 | + |
| 23 | + using dist_type = std::uniform_int_distribution<local_result_type>; |
| 24 | + |
| 25 | + std::random_device dev { }; |
19 | 26 |
|
20 | | - std::random_device device; |
21 | | - const local_result_type seed(device()); |
22 | | - std::mt19937 gtor(seed); |
23 | | - std::uniform_int_distribution<local_result_type> distribution |
24 | | - { |
25 | | - static_cast<local_result_type>(UINT16_C(1)), |
26 | | - static_cast<local_result_type>(UINT16_C(1023)) |
27 | | - }; |
| 27 | + const local_result_type seed(dev()); |
| 28 | + EngineType eng { seed }; |
| 29 | + |
| 30 | + dist_type |
| 31 | + distribution |
| 32 | + { |
| 33 | + static_cast<local_result_type>(UINT16_C(1)), |
| 34 | + static_cast<local_result_type>(UINT16_C(1023)) |
| 35 | + }; |
| 36 | + |
| 37 | + std::stringstream strm { }; |
28 | 38 |
|
29 | 39 | // Print the seed. |
30 | | - std::cout << "Seed is: 0x" |
31 | | - << std::hex |
32 | | - << std::setw(8) |
33 | | - << std::setfill('0') |
34 | | - << std::uppercase |
35 | | - << seed |
36 | | - << ". "; |
| 40 | + strm << "Seed is: 0x" |
| 41 | + << std::hex |
| 42 | + << std::setw(8) |
| 43 | + << std::setfill('0') |
| 44 | + << std::uppercase |
| 45 | + << seed |
| 46 | + << ". "; |
37 | 47 |
|
38 | 48 | // Generate 3 pseudo-random numbers in [1, 1023]. |
39 | | - const unsigned random_numbers[3U] = |
| 49 | + const unsigned random_numbers[std::size_t { UINT8_C(3) }] = |
40 | 50 | { |
41 | | - distribution(gtor), |
42 | | - distribution(gtor), |
43 | | - distribution(gtor) |
| 51 | + static_cast<unsigned>(distribution(eng)), |
| 52 | + static_cast<unsigned>(distribution(eng)), |
| 53 | + static_cast<unsigned>(distribution(eng)) |
44 | 54 | }; |
45 | 55 |
|
46 | | - std::cout << "Random numbers in [1, 1023]: "; |
47 | | - std::cout << std::dec << std::setw(5) << std::setfill(char(' ')) << random_numbers[0U] << ", "; |
48 | | - std::cout << std::dec << std::setw(5) << std::setfill(char(' ')) << random_numbers[1U] << ", "; |
49 | | - std::cout << std::dec << std::setw(5) << std::setfill(char(' ')) << random_numbers[2U] << std::endl; |
| 56 | + strm << "Random numbers in [1, 1023]: "; |
| 57 | + strm << std::dec << std::setw(5) << std::setfill(char(' ')) << random_numbers[0U] << ", "; |
| 58 | + strm << std::dec << std::setw(5) << std::setfill(char(' ')) << random_numbers[1U] << ", "; |
| 59 | + strm << std::dec << std::setw(5) << std::setfill(char(' ')) << random_numbers[2U]; |
| 60 | + |
| 61 | + std::cout << strm.str() << std::endl; |
50 | 62 | } |
51 | 63 |
|
52 | 64 | auto main() -> int |
53 | 65 | { |
54 | | - // Generate 20 sequences of 3 pseudo-random numbers. |
55 | | - for(std::uint_fast8_t i = 0U; i < 20U; ++i) |
| 66 | + // Generate 8 sequences of 3 pseudo-random numbers. |
| 67 | + for(std::uint_fast8_t i = { UINT8_C(0) }; i < std::uint_fast8_t { UINT8_C(8) }; ++i) |
56 | 68 | { |
57 | | - do_something(); |
| 69 | + // For std::mt19937. |
| 70 | + using eng32_type = std::mt19937; |
| 71 | + |
| 72 | + do_something<eng32_type>(); |
| 73 | + } |
| 74 | + |
| 75 | + std::cout << std::endl; |
| 76 | + |
| 77 | + // Generate 8 sequences of 3 pseudo-random numbers. |
| 78 | + for(std::uint_fast8_t i = { UINT8_C(0) }; i < std::uint_fast8_t { UINT8_C(8) }; ++i) |
| 79 | + { |
| 80 | + // For std::mt19937_64. |
| 81 | + using eng64_type = std::mt19937_64; |
| 82 | + |
| 83 | + do_something<eng64_type>(); |
58 | 84 | } |
59 | 85 | } |
0 commit comments