Added explanatory comment in primary function

This commit is contained in:
Bob Polis 2022-03-31 16:00:04 +02:00
parent 4e656db597
commit b0ea16a96d

View File

@ -1,11 +1,3 @@
//
// main.cpp
// ranlin
//
// Created by Bob Polis on 05-03-15.
// Copyright (c) 2015 Thalictrum. All rights reserved.
//
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string> #include <string>
@ -20,9 +12,13 @@ double next_random() {
} }
string random_line_from_file(istream& file) { 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 line;
string result; string result;
for (int nr {1}; getline(file, line); ++nr) { for (int nr = 1; getline(file, line); ++nr) {
if (next_random() * nr < 1.0) { if (next_random() * nr < 1.0) {
result = line; result = line;
} }