diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2b7380d --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +CC=gcc +CXX=g++ +RM=rm -f +CP=cp +MKDIR=mkdir -p +CPPFLAGS=-Wall -O2 -std=c++11 +LDFLAGS= +LDLIBS= + +TOOL=ranlin +SRCS=main.cpp +OBJS=$(subst .cpp,.o,$(SRCS)) + +all: $(TOOL) + +$(TOOL): $(OBJS) + $(CXX) $(LDFLAGS) -o $(TOOL) $(LDLIBS) $(OBJS) + +depend: .depend + +.depend: $(SRCS) + $(RM) ./.depend + $(CXX) $(CPPFLAGS) -MM $^>>./.depend; + +clean: + $(RM) $(OBJS) + +dist-clean: clean + $(RM) *~ ./.depend $(TOOL) + +install: + $(RM) ~/bin/$(TOOL) + $(CP) $(TOOL) ~/bin/ + +include .depend diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b9d49d3 --- /dev/null +++ b/main.cpp @@ -0,0 +1,40 @@ +// +// main.cpp +// ranlin +// +// Created by Bob Polis on 05-03-15. +// Copyright (c) 2015 Thalictrum. All rights reserved. +// + +#include +#include +#include +#include +using namespace std; + +double next_random() { + static random_device dev; + static default_random_engine engine {dev()}; + static uniform_real_distribution dist {0, 1}; + return dist(engine); +} + +string random_line_from_file(istream& file) { + 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'; + } +}