fork(2) download
  1. #include <iomanip>
  2. #include <limits>
  3. #include <random>
  4. #include <iostream>
  5.  
  6. int main()
  7. {
  8. // read two real numbers
  9. double a, b;
  10. if (!(std::cin >> a >> b))
  11. std::exit(EXIT_FAILURE);
  12.  
  13. // generate random number in [a, b] range
  14. std::random_device rd;
  15. std::mt19937 gen(rd());
  16. double nextafter_b = std::nextafter(b, std::numeric_limits<decltype(b)>::max());
  17. std::uniform_real_distribution<> dis(a, nextafter_b);
  18. double r = dis(gen);
  19.  
  20. // output with 2 decimal points
  21. std::cout << std::fixed << std::setprecision(2) << r;
  22. }
Success #stdin #stdout 0s 16064KB
stdin
1.2345
6.789
stdout
5.19