/* CSCI 200: Lab 1B (Random Classification): Tyler Beckman * * Author: Tyler Beckman * * A program to generate a random number between user inputted bounds * (inclusive), and classify it by quartile. In addition, more than one random * number in the same range can be generated if the user wishes. */ #include #include #include #include #include float input_float(std::string prompt) { float out; while (true) { std::cout << prompt + ": "; std::cin >> out; if (std::cin.fail()) { // The clear and ignore are necessary because otherwise it seems to // keep reusing the first input a person enters std::cin.clear(); std::cin.ignore(std::numeric_limits::max(), '\n'); std::cout << "Invalid number, please make sure your number is formatted " "correctly." << std::endl; } else { break; } } return out; } int main(void) { float min = input_float("Please input the minimum value"), max = input_float("Please input the maximum value"); std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count()); std::uniform_real_distribution realDist( min, std::nextafter(max, std::numeric_limits::max())); while (true) { float randomNumber = realDist(mt); std::cout << "The random value is: " << randomNumber << std::endl; float quartileSize = (max - min) / 4.0f; if (randomNumber < (min + quartileSize)) { std::cout << "This number is in the first quartile" << std::endl; } else if (randomNumber < (min + quartileSize) * 2) { std::cout << "This number is in the second quartile" << std::endl; } else if (randomNumber < (min + quartileSize) * 3) { std::cout << "This number is in the third quartile" << std::endl; } else { std::cout << "This number is in the fourth quartile" << std::endl; } char runAgainInput; std::cout << "Do you want another random value? (Y/N) "; std::cin >> runAgainInput; if (runAgainInput != 'Y' && runAgainInput != 'y') { std::cout << "Have a nice day!" << std::endl; break; } } }