Skip to content

Instantly share code, notes, and snippets.

@minecrawler
Created August 8, 2017 13:12
Show Gist options
  • Save minecrawler/18767938683936254eb4c173b66e2da4 to your computer and use it in GitHub Desktop.
Save minecrawler/18767938683936254eb4c173b66e2da4 to your computer and use it in GitHub Desktop.
Guess random number game
#include <chrono>
#include <iostream>
#include <random>
#include <string>
using namespace std;
using namespace std::chrono;
int main() {
int playerGuess = 0;
string playerName;
int playerTries = 0;
cout << "Welcome, player! Please enter your name: ";
getline(cin, playerName);
random_device rd;
milliseconds ms = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
std::default_random_engine gen(rd() + ms.count());
std::uniform_int_distribution<int> dis(1, 100);
int myNumber = dis(gen);
cout << endl << "So, " << playerName << ", I thought of a number between 1 and 100." << endl;
while (1) {
playerTries++;
cout << "Please try to guess the number: ";
cin >> playerGuess;
if (playerGuess == myNumber) break;
cout << "My number is " << (playerGuess < myNumber ? "bigger" : "smaller") << " than your guess!" << endl;
}
cout << endl << "Congratulations! That's the correct number." << endl;
cout << "It took you " << playerTries << " guesses to find it :)" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment