37 lines
959 B
C++
37 lines
959 B
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <random>
|
|
using namespace std;
|
|
|
|
double next_random() {
|
|
static random_device dev;
|
|
static default_random_engine engine {dev()};
|
|
static uniform_real_distribution<double> dist {0, 1};
|
|
return dist(engine);
|
|
}
|
|
|
|
string random_line_from_file(istream& file) {
|
|
// This function selects a line from a file by reading
|
|
// the file line by line, so the file is never in memory
|
|
// as a whole. Still, the chance a line is being picked
|
|
// is equal for every line.
|
|
string line;
|
|
string result;
|
|
for (int nr = 1; getline(file, line); ++nr) {
|
|
if (next_random() * nr < 1.0) {
|
|
result = line;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int main(int argc, const char * argv[]) {
|
|
if (argc > 1) {
|
|
ifstream infile {argv[1]};
|
|
cout << random_line_from_file(infile) << '\n';
|
|
} else {
|
|
cout << random_line_from_file(cin) << '\n';
|
|
}
|
|
}
|