JayBeams  0.1
Another project to have fun coding.
check_mt19937_initializer.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  *
4  * Generate some output from std::random_device.
5  *
6  * I am interested in examining the output from std::random_device to
7  * evaluate its statistical strength.
8  */
10 #include <jb/config_object.hpp>
11 
12 #include <iostream>
13 #include <random>
14 
15 /**
16  * Define types and functions used in the program.
17  */
18 namespace {
19 /// Configuration parameters for bm_order_book
20 class config : public jb::config_object {
21 public:
22  config();
24  void validate() const override;
28 };
29 
30 void produce_output(config const& cfg) {
31  auto generator = jb::testing::initialize_mersenne_twister<std::mt19937_64>(
32  cfg.seed(), cfg.token());
33 
34  for (int i = 0; i != cfg.iterations(); ++i) {
35  std::cout << std::uniform_real_distribution<>(0, 1)(generator) << "\n";
36  }
37 }
38 
39 } // anonymous namespace
40 
41 int main(int argc, char* argv[]) try {
42  config cfg;
43  cfg.process_cmdline(argc, argv);
44 
45  produce_output(cfg);
46  return 0;
47 } catch (jb::usage const& ex) {
48  std::cerr << "usage: " << ex.what() << std::endl;
49  return ex.exit_status();
50 } catch (std::exception const& ex) {
51  std::cerr << "standard exception raised: " << ex.what() << std::endl;
52  return 1;
53 } catch (...) {
54  std::cerr << "unknown exception raised" << std::endl;
55  return 1;
56 }
57 
58 namespace {
59 namespace defaults {
60 #ifndef JB_ITCH5_DEFAULT_check_random_device_token
61 #define JB_ITCH5_DEFAULT_check_random_device_token \
62  jb::testing::default_initialization_marker
63 #endif // JB_ITCH5_DEFAULT_check_random_device_token
64 
65 #ifndef JB_ITCH5_DEFAULT_check_random_device_iterations
66 #define JB_ITCH5_DEFAULT_check_random_device_iterations 10000
67 #endif // JB_ITCH5_DEFAULT_check_random_device_iterations
68 
69 std::string const token = JB_ITCH5_DEFAULT_check_random_device_token;
71 
72 } // namespace defaults
73 
74 config::config()
75  : iterations(
76  desc("iterations")
77  .help(
78  "Define how many values to extract from std::random_device."),
79  this, defaults::iterations)
80  , token(
81  desc("token").help(
82  "Define the parameter to initialize the random device."
83  "The semantics are implementation defined, but on libc++ and "
84  "libstd++ the value is the name of a device to read, such as "
85  "'/dev/random' or '/dev/urandom'"),
86  this, defaults::token)
87  , seed(
88  desc("seed").help(
89  "If non-zero the generator is initialized using this seed."),
90  this, 0) {
91 }
92 
93 void config::validate() const {
94  if (iterations() <= 0) {
95  std::ostringstream os;
96  os << "--iterations (" << iterations() << ") must be >= 0";
97  throw jb::usage{os.str(), 1};
98  }
99 }
100 
101 } // namespace config
Define defaults for program parameters.
Base class for all configuration objects.
virtual void validate() const
Validate the settings.
int exit_status() const
Definition: usage.hpp:21
#define config_object_constructors(NAME)
Helper class to easily define configuration attributes.
A simple class to communicate the result of parsing the options.
Definition: usage.hpp:11
#define JB_ITCH5_DEFAULT_check_random_device_iterations
#define JB_ITCH5_DEFAULT_check_random_device_token
int main(int argc, char *argv[])