randomly.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * cxxomfort example: random, foreach
  3. *
  4. * This program provides sample usage of the
  5. * "foreach loop" emulation feature.
  6. */
  7. //#define CXXOMFORT_NOTICES 1
  8. #include <cxxomfort/backports.hpp>
  9. #include <cxxomfort/random.hpp>
  10. #include <cxxomfort/library/random.hpp>
  11. //#include <cxxomfort/library/foreach.hpp> // foreach
  12. #include <cxxomfort/library/type_name.hpp>
  13. #include <cxxomfort/library/numeric.hpp> // make_unsigned
  14. #include <cxxomfort/library/fixed_vector.hpp>
  15. #include <cxxomfort/library/type_name.hpp>
  16. #include <vector>
  17. #include <list>
  18. #include <deque>
  19. #include <iterator>
  20. #include <iostream>
  21. #include <sstream>
  22. #include <iomanip>
  23. #include <ctime> // time (), srand ()
  24. /*
  25. * The demonstration consists of:
  26. *
  27. * * generating an amount {LIM} of random numbers
  28. * * setting up a functoid that will "tick" with 1/{PQ} probability when invoked
  29. * * iteratively erase from the list with the above probability functoid
  30. * * until there's at most {LLOW} elements left
  31. *
  32. */
  33. const unsigned int LIM = 1000;
  34. const unsigned int LLOW = 20;
  35. const int NMAX = 0x8000;
  36. const unsigned PQ = 1;
  37. const unsigned PR= 9;
  38. struct gen {
  39. std::minstd_rand ms;
  40. std::mt19937 mt;
  41. std::ranlux24_base r24b;
  42. // msvc shuffle_order_engine is broken so we use a different knuth_b
  43. cxxomfort::fix::knuth_b kn;
  44. };
  45. // pq_selector
  46. const struct pq_selector_t {
  47. bool operator() (int) const {
  48. double pq= ((double)rand()/RAND_MAX);
  49. return pq * PR < PQ;
  50. }
  51. } pqs = {};
  52. namespace num= cxxomfort::library::numeric;
  53. namespace ran= cxxomfort::library::random;
  54. int main () {
  55. using namespace std;
  56. cxxomfort::output_info(stdout); cout<< endl;
  57. srand (time(0));
  58. cout<< "COUNTER "<< hex<< __COUNTER__<< dec<< endl;
  59. random_device rd;
  60. // example containers
  61. vector<unsigned short> ve (LIM);
  62. list<unsigned short> li (LIM);
  63. //forward_list<unsigned short> fl (LIM);
  64. deque<unsigned short> dq (LIM);
  65. cxxomfort::library::fixed_vector<unsigned short> fve (LIM);
  66. #define THISSEQ() dq
  67. cout<< "Selected sequence is: "<< cxxomfort::library::typeid_demangle(typeid(THISSEQ()))<< endl;
  68. CXXO_I12N_SEQ( vector<short>, samples, {1, -1, 2, -2, 3, -3, 4, -4, 5, -5 });
  69. vector<unsigned short> seq (LIM);
  70. // example generators
  71. minstd_rand0 rg_0 (time(0));
  72. ranlux24_base rg_r (time(0));
  73. ranlux48_base rg_r2 (time(0));
  74. ran::icg0 rg_inv (time(0));
  75. cxxomfort::fix::knuth_b rg_k( num::to_unsigned(time(0)) ^ CXXOMFORT_COMPILER_VERSION );
  76. #define THISRNG() rg_inv
  77. uniform_int_distribution<unsigned short> dis(1, NMAX);
  78. // generate a list of {LIM} elements obtained from one of the
  79. // software generators
  80. //function<unsigned short()> rn = cxxomfort::library::make_random_wrap(dis, mt);
  81. generate(begin(THISSEQ()), end(THISSEQ()), bind(dis, THISRNG() ) );
  82. CXXO_FOREACH (int n, THISSEQ() ) { cout<< hex<< n<< " "; }
  83. cout<< dec<< endl;
  84. // filter the sequence with a selector that has
  85. // probability 0 < PQ/PR < 1 of matching
  86. // filter sequence x times until less than {LLOW} elements remain
  87. size_t pass= 0;
  88. size_t oldsize= size(THISSEQ()), newsize= oldsize;
  89. while (size(THISSEQ()) >= LLOW) {
  90. //vector<unsigned short>::iterator ;
  91. THISSEQ().erase( remove_if(begin(THISSEQ()), end(THISSEQ()), pqs), end(THISSEQ()));
  92. oldsize= newsize;
  93. newsize= seq.size();
  94. ++pass;
  95. cout<< "Pass "<< setw(3)<< pass<< " Reduced "
  96. << static_cast<int>(((double)newsize/oldsize)*100)<< "% "
  97. << "Remain "<< size(THISSEQ())<< endl;
  98. }
  99. CXXO_FOREACH (int n, THISSEQ() ) { cout<< hex<< n<< " "; }
  100. cout<< endl;
  101. }