Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:13

0001 // name=do_while_continue ; gcc $name.cc -std=c++11 -lstdc++ -o /tmp/$name && /tmp/$name
0002 /**
0003 https://stackoverflow.com/questions/9878965/rand-between-0-and-1
0004 **/
0005 #include <iostream>
0006 #include <random>
0007 #include <chrono>
0008 
0009 bool do_while_condition(double a, double b)
0010 {
0011     std::cout << "do_while_condition a " << a << " b " << b  << std::endl ; 
0012     return true ; 
0013 }
0014 
0015 int main()
0016 {
0017     std::mt19937_64 rng;
0018     uint64_t timeSeed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
0019     std::seed_seq ss{uint32_t(timeSeed & 0xffffffff), uint32_t(timeSeed>>32)};
0020     rng.seed(ss);
0021     std::uniform_real_distribution<double> unif(0, 1);
0022 
0023     bool continue_bug = true ; 
0024 
0025     double a ; 
0026     double b ; 
0027 
0028     do {
0029         a = unif(rng);
0030         std::cout << "head a " << a << std::endl;
0031      
0032         if( a < 0.5 && continue_bug  )
0033         {
0034             std::cout << "continue " << std::endl ; 
0035             continue ;   
0036             // GOTO WHILE CONDITION POTENTIALLY USING b UNINITIALIZED 
0037             // SO IN GENERAL WHAT THIS CODE DOES IS UNDEFINED
0038         }
0039 
0040         b = unif(rng); 
0041         std::cout << "tail b " << b << std::endl ; 
0042 
0043     } while( ( a < 0.9 || b < 0.9 ) && do_while_condition(a,b) ) ; 
0044 
0045     // looping condition 
0046 
0047     std::cout 
0048         << " result " 
0049         << " a " << a 
0050         << " b " << b 
0051         << std::endl
0052         ; 
0053 
0054     return 0;
0055 }
0056 
0057