fork(2) download
  1. #include <cmath>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <iostream>
  5. #include <limits>
  6.  
  7. int roll(int mini, int maxi)
  8. {
  9. int v = maxi - mini;
  10. int x = mini + (rand() % (v+1));
  11. return x;
  12.  
  13. }
  14.  
  15. void flush_stream(std::istream& stream)
  16. {
  17. stream.clear();
  18. stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  19. }
  20.  
  21. int get_valid_number(const std::string& prompt)
  22. {
  23. int number = 0;
  24.  
  25. bool valid = false;
  26. while (!valid)
  27. {
  28. std::cout << prompt << std::endl;
  29. if (std::cin >> number)
  30. {
  31. valid = true;
  32. }
  33. flush_stream(std::cin);
  34. }
  35.  
  36. return number;
  37. }
  38.  
  39. void caller()
  40. {
  41. const int a = get_valid_number("Enter minimum number");
  42. int b = std::numeric_limits<int>::min();
  43. do
  44. {
  45. b = get_valid_number("Enter maximum number");
  46. }
  47. while (b <= a);
  48. const int c = get_valid_number("How many rolls?");
  49.  
  50. for (int i = 0; i < c; ++i)
  51. {
  52. std::cout << roll(a, b) << std::endl;
  53. }
  54. }
  55.  
  56. int main()
  57. {
  58. srand(time(NULL));
  59. caller();
  60. }
  61.  
Success #stdin #stdout 0s 2904KB
stdin
1
6
10
stdout
Enter minimum number
Enter maximum number
How many rolls?
1
1
1
2
4
5
1
4
3
1