fork download
  1. #include <cmath>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <string>
  5. #include <experimental/random>
  6.  
  7. using std::experimental::randint;
  8.  
  9. int main()
  10. {
  11. // read two numbers specified with 2 decimal places
  12. std::string a, b;
  13. if (!(std::cin >> a >> b))
  14. std::exit(EXIT_FAILURE);
  15.  
  16. // "1.23" -> Int(123)
  17. auto two_places = [](std::string s) -> long long {
  18. s += "00"; // pad with zeros for 1. -> 100 case
  19. auto i = s.find('.');
  20. if (i != std::string::npos) { // found decimal point
  21. s = s.substr(0, i) + s.substr(i+1, 2); // drop decimal point
  22. }
  23. return std::stoll(s);
  24. };
  25. long long r = randint(two_places(a), two_places(b));
  26. unsigned long long ur = std::abs(r);
  27. std::cout << (r < 0 ? "-" : "") << (ur / 100) << '.'
  28. << std::setfill('0') << std::setw(2) << (ur % 100);
  29. }
Success #stdin #stdout 0s 4388KB
stdin
-92233720368547758.08
-92233720368547758.08
stdout
-92233720368547758.08