TF++, Miscellaneous classes and modules in C++:
rngtest.cc
Go to the documentation of this file.
1 
18 #define RNGTEST_VERSION \
19  "RNGTEST V1.0 test random number generator module"
20 
21 #include <iostream>
22 #include <tfxx/commandline.h>
23 #include <tfxx/rng.h>
24 
25 using std::cout;
26 using std::cerr;
27 using std::endl;
28 
29 /* ---------------------------------------------------------------------- */
30 
31 struct Options {
32  double mean,std;
33  unsigned long int seed;
35  std::string type;
36  int nsamples;
37 }; // struct Options
38 
39 /* ---------------------------------------------------------------------- */
40 
42 {
43  cout << "RNG parameters:\n"
44  << " type: " << rng.type() << "\n"
45  << " seed: " << rng.seed() << "\n"
46  << " mean: " << rng.mean() << "\n"
47  << " std: " << rng.std() << endl;
48 } // void report(const tfxx::numeric::RNGgaussian& rng)
49 
50 /* ---------------------------------------------------------------------- */
51 
52 int main(int iargc, char* argv[])
53 {
54 
55  // define usage information
56  char usage_text[]=
57  {
58  RNGTEST_VERSION "\n"
59  "usage: rngtest [-l] [-v] [-t type] [-seed v] [-mean v]\n"
60  " [-std v] [-n n] [run]\n"
61  " or: rngtest --help|-h" "\n"
62  " or: rngtest --xhelp" "\n"
63  };
64 
65  // define full help text
66  char help_text[]=
67  {
68  "--xhelp print comments regarding GSL RNG implementation\n"
69  "\n"
70  "run run the program\n"
71  "-l print list of random number generators and exit\n"
72  "-v be verbose\n"
73  "-t type select random number generator of type \"type\"\n"
74  " use option -l for a list of available types\n"
75  " type \"default\" will make selection based on\n"
76  " environment variables\n"
77  "-seed v initialize RNG with seed value v\n"
78  "-mean v set mean value\n"
79  "-std v set standard deviation\n"
80  "-n n print n random numbers\n"
81  "\n"
82  "In case of no type selected or type set to \"default\" type\n"
83  "and seed value can be specified through environment variables.\n"
84  "Simply try\n"
85  "\n"
86  "GSL_RNG_TYPE=ranlux GSL_RNG_SEED=125 rngtest -v run\n"
87  };
88 
89  // define commandline options
90  using namespace tfxx::cmdline;
91  static Declare options[]=
92  {
93  // 0: print help
94  {"help",arg_no,"-"},
95  // 1: verbose mode
96  {"v",arg_no,"-"},
97  // 2: print list of random number generators
98  {"l",arg_no,"-"},
99  // 3: type of RNG
100  {"t",arg_yes,"default"},
101  // 4: seed value
102  {"seed",arg_yes,"0."},
103  // 5: mean value
104  {"mean",arg_yes,"0."},
105  // 6: standard deviation
106  {"std",arg_yes,"1."},
107  // 7: number of samples to be printed
108  {"n",arg_yes,"10"},
109  // 8: print comment text
110  {"xhelp",arg_no,"-"},
111  {NULL}
112  };
113 
114  // no arguments? print usage...
115  if (iargc<2)
116  {
117  cerr << usage_text << endl;
118  exit(0);
119  }
120 
121  // collect options from commandline
122  Commandline cmdline(iargc, argv, options);
123 
124  // help requested? print full help text...
125  if (cmdline.optset(0) || cmdline.optset(8))
126  {
127  cerr << usage_text << endl;
128  cerr << help_text << endl;
129  if (cmdline.optset(8))
130  {
131  cerr << endl;
133  }
134  exit(0);
135  }
136 
137  Options opt;
138  opt.verbose=cmdline.optset(1);
139  opt.listtypes=cmdline.optset(2);
140  opt.type=cmdline.string_arg(3);
141  opt.seedset=cmdline.optset(4);
142  opt.seed=cmdline.long_arg(4);
143  opt.mean=cmdline.double_arg(5);
144  opt.std=cmdline.double_arg(6);
145  opt.nsamples=cmdline.int_arg(7);
146 
147  if (opt.listtypes)
148  {
150  }
151  else
152  {
153  cout << RNGTEST_VERSION << endl;
154  if (opt.verbose)
155  {
156  cout << "Parameters specified on command line (maybe default):\n"
157  << " type: " << opt.type << "\n"
158  << " mean: " << opt.mean << "\n"
159  << " std: " << opt.std << "\n"
160  << " n: " << opt.nsamples << endl;
161  if (opt.seedset)
162  {
163  cout << " seed: " << opt.seed << endl;
164  }
165  else
166  {
167  cout << " no seed value specified" << endl;
168  }
169  }
170  // create RNG
171  tfxx::numeric::RNGgaussian rng(opt.std, opt.mean, opt.type.c_str());
172  if (opt.seedset) { rng.set(opt.seed); }
173  // report
174  if (opt.verbose) { report(rng); }
175 
176  cout << "\nRandom numbers:" << endl;
177  for (int i=0; i<opt.nsamples; ++i)
178  {
179  cout << i << " " << rng.value() << endl;
180  }
181 
182  cout << endl << "set new seed" << endl;
183  rng.set();
184  if (opt.verbose) { report(rng); }
185 
186  cout << "\nRandom numbers:" << endl;
187  for (int i=0; i<opt.nsamples; ++i)
188  {
189  cout << i << " " << rng.value() << endl;
190  }
191  }
192 }
193 
194 /* ----- END OF rngtest.cc ----- */
double mean
Definition: rngtest.cc:32
int int_arg(const int &iopt) const
return argument of option # iopt as int value
Definition: commandline.h:219
option requires an argument
Definition: commandline.h:102
bool optset(const int &iopt) const
true if option # iopt was set on commandline
Definition: commandline.h:213
double double_arg(const int &iopt) const
return argument of option # iopt as double value
Definition: commandline.h:225
double mean() const
return mean
Definition: rng.h:100
unsigned long int seed() const
return last seed value
Definition: rng.h:96
std::string type() const
return type of random number generator
Definition: rng.h:94
long long_arg(const int &iopt) const
return argument of option # iopt as long value
Definition: commandline.h:228
std::string type
Definition: rngtest.cc:35
int main(int iargc, char *argv[])
Definition: rngtest.cc:52
static void rng_list_types(std::ostream &os)
print list of random number generators to stream.
Definition: rng.cc:122
void report(const tfxx::numeric::RNGgaussian &rng)
Definition: rngtest.cc:41
bool seedset
Definition: rngtest.cc:34
bool verbose
Definition: rngtest.cc:34
int nsamples
Definition: rngtest.cc:36
Namespace containing all components of module commandline. ,.
Definition: commandline.cc:41
Evaluates commandline by calling long_getopt. ,You may instantiate a Commandline object by passing th...
Definition: commandline.h:201
Provide random numbers with Gaussian distribution.This class is an interface to the GSL random number...
Definition: rng.h:78
unsigned long int seed
Definition: rngtest.cc:33
double std
Definition: rngtest.cc:32
static const char * comment_gsl_rng_ranlux
comment on GSL RNGs
Definition: rng.h:104
#define RNGTEST_VERSION
Definition: rngtest.cc:18
double std() const
return standard deviation
Definition: rng.h:98
struct to define options ,This struct is used to define a list of options. An example is: ...
Definition: commandline.h:136
option has no argument
Definition: commandline.h:100
std::string string_arg(const int &iopt) const
return argument of option # iopt as string value
Definition: commandline.h:216
bool listtypes
Definition: rngtest.cc:34
void set(const unsigned long int &seed)
feed with seed value
Definition: rng.cc:104