April 7, 2012, 2:42 pm
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <vector>
using namespace std;
void getrandline(string filename,size_t &selected_line_no,string &selected_line) {
ifstream file(filename.c_str());
selected_line_no = 0;
for(size_t n=1;!file.eof();n++) {
string current_line;
getline(file,current_line);
if(file.eof()) break;
if(rand()%n == 0) { selected_line = current_line; selected_line_no = n-1; }
}
}
int main(int argc,char **argv) {
srand(time(NULL));
vector<size_t> count(100,0); // just for testing, 100 should be > size of the file...
for(size_t n=0;n<100000;n++) {
size_t linenum;
string line;
getrandline(argv[1],linenum,line);
count[linenum]++;
cout << linenum << " " << line << endl;
}
for(size_t n=0;n<count.size();n++) {
cout << n << " " << count[n] << endl;
}
}