/**************************************************************** **** **** This file belongs with the course **** Introduction to Scientific Programming in C++/Fortran2003 **** copyright 2016-2023 Victor Eijkhout eijkhout@tacc.utexas.edu **** **** uniform.cxx : trying to demonstrate the difference between **** two random number generators **** DOES NOT WORK **** ****************************************************************/ #include using std::cin; using std::cout; #include using std::function; #include using std::vector; #include // #include "rand.h" #include "cxxopts.hpp" int main(int argc,char **argv) { cxxopts::Options options("uniform", "Detect uniform random"); options.add_options() ("h,help","usage information") ("b,buckets","number of buckets", cxxopts::value()->default_value("4096")) ("s,samples","number of samples", cxxopts::value()->default_value("1000")) ; auto result = options.parse(argc, argv); if (result.count("help")>0) { std::cout << options.help() << '\n'; return 0; } int nbuckets = result["buckets"].as(); int nsamples = result["samples"].as(); for ( auto strategy : {1,2} ) { function< int() > random_number; std::uniform_int_distribution<> distribution(0,nbuckets-1); std::default_random_engine generator; if (strategy==1) random_number = [nbuckets] () { return rand() % nbuckets; }; else { random_number = [&generator,&distribution] () { return distribution(generator); }; } vector buckets( nbuckets); for (int i=0; ipos) pos = d; if (-d>neg) neg = -d; } ); cout << "Deviation over=" << pos << ", under=" << neg << '\n'; } return 0; }