JayBeams  0.1
Another project to have fun coding.
check_random_device.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  */
9 #include <jb/config_object.hpp>
10 
11 #include <iostream>
12 #include <random>
13 
14 /**
15  * Define types and functions used in the program.
16  */
17 namespace {
18 /// Configuration parameters for bm_order_book
19 class config : public jb::config_object {
20 public:
21  config();
23  void validate() const override;
26 };
27 
28 void produce_output(std::random_device& rd, int iterations) {
29  for (int i = 0; i != iterations; ++i) {
30  std::cout << rd() << "\n";
31  }
32 }
33 
34 char const default_initialization_marker[] = "__default__";
35 
36 } // anonymous namespace
37 
38 int main(int argc, char* argv[]) try {
39  config cfg;
40  cfg.process_cmdline(argc, argv);
41 
42  if (cfg.token() == default_initialization_marker) {
43  std::random_device rd;
44  produce_output(rd, cfg.iterations());
45  } else {
46  std::random_device rd(cfg.token());
47  produce_output(rd, cfg.iterations());
48  }
49  return 0;
50 } catch (jb::usage const& ex) {
51  std::cerr << "usage: " << ex.what() << std::endl;
52  return ex.exit_status();
53 } catch (std::exception const& ex) {
54  std::cerr << "standard exception raised: " << ex.what() << std::endl;
55  return 1;
56 } catch (...) {
57  std::cerr << "unknown exception raised" << std::endl;
58  return 1;
59 }
60 
61 namespace {
62 namespace defaults {
63 #ifndef JB_ITCH5_DEFAULT_check_random_device_token
64 #define JB_ITCH5_DEFAULT_check_random_device_token default_initialization_marker
65 #endif // JB_ITCH5_DEFAULT_check_random_device_token
66 
67 #ifndef JB_ITCH5_DEFAULT_check_random_device_iterations
68 #define JB_ITCH5_DEFAULT_check_random_device_iterations 1000
69 #endif // JB_ITCH5_DEFAULT_check_random_device_iterations
70 
71 std::string const token = JB_ITCH5_DEFAULT_check_random_device_token;
73 
74 } // namespace defaults
75 
76 config::config()
77  : iterations(
78  desc("iterations")
79  .help(
80  "Define how many values to extract from std::random_device."),
81  this, defaults::iterations)
82  , token(
83  desc("token").help(
84  "Define the parameter to initialize the random device."
85  "The semantics are implementation defined, but on libc++ and "
86  "libstd++ the value is the name of a device to read, such as "
87  "'/dev/random' or '/dev/urandom'"),
88  this, defaults::token) {
89 }
90 
91 void config::validate() const {
92  if (iterations() <= 0) {
93  std::ostringstream os;
94  os << "--iterations (" << iterations() << ") must be >= 0";
95  throw jb::usage{os.str(), 1};
96  }
97 }
98 
99 } // 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 JB_ITCH5_DEFAULT_check_random_device_token
#define config_object_constructors(NAME)
#define JB_ITCH5_DEFAULT_check_random_device_iterations
Helper class to easily define configuration attributes.
char const default_initialization_marker[]
A simple class to communicate the result of parsing the options.
Definition: usage.hpp:11
int main(int argc, char *argv[])