Skip to content

Instantly share code, notes, and snippets.

@zmalltalker
Last active February 19, 2021 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmalltalker/63ba3a853cfa43a9b174d24d946ac6b7 to your computer and use it in GitHub Desktop.
Save zmalltalker/63ba3a853cfa43a9b174d24d946ac6b7 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <cctype>
#include <fstream>
#include <iostream>
#include <list>
using namespace std;
string random_word(string path) {
ifstream file(path);
ifstream fileForRead(path);
string result;
string s;
file.unsetf(ios_base::skipws);
unsigned line_count =
count(istream_iterator<char>(file), istream_iterator<char>(), '\n');
/* initialize random seed: */
srand(time(NULL));
/* generate secret number between 1 and number of lines in the file: */
int iSecret = rand() % line_count + 1;
int i = 0;
while (getline(fileForRead, s)) {
if (i == iSecret) {
result = s;
}
++i;
}
return result;
}
int main(int argc, char *argv[]) {
string noun = random_word(
"/Users/marius/tmp/heroku-name-generator/dictionaries/nouns.txt");
string adjective = random_word(
"/Users/marius/tmp/heroku-name-generator/dictionaries/adjectives.txt");
transform(noun.begin(), noun.end(), noun.begin(), ::tolower);
transform(adjective.begin(), adjective.end(), adjective.begin(), ::tolower);
cout << adjective << "-" << noun << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment