Can LaTeX select a random line from another file?

Can LaTeX select a line from one file and include it in the main file? I use the exam class to make course exams. I like to include a brief quote related to the course in the footer of the last exam page. Currently, I type a new quote each time. I would like to assemble a separate file of quotes, then have one quote drawn at random from the file and placed into the footer when I compile the exam. Quotes may occupy two footer lines but would appear on a single line in the quote file.

\documentclass{exam}

\footer{\iflastpage{\footnotesize{Random pithy quote goes here.'' --- Pithy quote speaker.}}}{}{}

\begin{document}
\begin{questions}

\question[15]
Can \LaTeX\ select a random quote from a file?

\end{questions}
\end{document}

-

You can maintain your list of quotes in a CSV file and then use datatool to extract the random quote:

\documentclass{exam}

\usepackage{filecontents,datatool}
% http://www.brainyquote.com/slideshow/topics/top_10_success_quotes.html
\begin{filecontents*}{quotes.txt}
Number,Quote,Author
1,"Success is not final, failure is not fatal: it is the courage to continue that counts.", Winston Churchill
2,"Coming together is a beginning; keeping together is progress; working together is success.", Henry Ford
3,"The price of success is hard work, dedication to the job at hand, and the determination that whether we win or lose, we have applied the best of ourselves to the task at hand.", Vince Lombardi
4,"Success consists of going from failure to failure without loss of enthusiasm.", Winston Churchill
5,"A successful man is one who can lay a firm foundation with the bricks others have thrown at him.", David Brinkley
6,"Always be yourself, express yourself, have faith in yourself, do not go out and look for a successful personality and duplicate it.", Bruce Lee
7,"To succeed in life, you need two things: ignorance and confidence.", Mark Twain
8,"Try not to become a man of success, but rather try to become a man of value.", Albert Einstein
9,"The starting point of all achievement is desire.", Napoleon Hill
10,"Whosoever desires constant success must change his conduct with the times.", Niccolo Machiavelli
\end{filecontents*}

\footer
{\iflastpage{%
\footnotesize%
\edef\RandomQuote{\number\numexpr1+\pdfuniformdeviate\DTLrowcount{quotes}}% Identify random quote
\dtlgetrow{quotes}{\RandomQuote}% Retrieve random quote
\dtlgetentryfromcurrentrow{\Number}{1}\dtlgetentryfromcurrentrow{\Quote}{2}\dtlgetentryfromcurrentrow{\Author}{3}%
\Number:~\Quote~---~\Author
}}% Left
{}% Centre
{}% Right

\begin{document}
\begin{questions}
\question[15]
Can \LaTeX{} select a random quote from a file?
\end{questions}
\end{document}

-
This is exactly what I was looking for. Thanks for introducing me to the datatool package. It may be helpful to me in other ways. – Mike Taylor Jun 28 '14 at 1:54

Solution Idea

Store the quotes in a text array using the arrayjob package, select one random quote by generating a random number using \pdfuniformdeviate.

\pdfuniformdeviate <num> generates a uniformly distributed random integer in the range [0, num). See this link for more details.

The Solution

\documentclass{exam}

\usepackage{arrayjob}
\usepackage{lipsum}

% Create an array to store the quotes
\newarray\pithyquotes

% Get random quote index
\newcounter{pqn}
\setcounter{pqn}{\pdfuniformdeviate 4}% The last number is the total number of quotes
\stepcounter{pqn}% In case 0 has been selected

\footer{\iflastpage{\footnotesize{\pithyquotes(\value{pqn})}}}{}{}

\begin{document}
\begin{questions}

\question[15]
Can \LaTeX\ select a random quote from a file?

\begin{solution}
Yes, of course.
\end{solution}

\end{questions}
\end{document}


The Output

-
I think the OP wants his quotes in a separate file. – Ethan Bolker Jun 28 '14 at 0:52
@EthanBolker Yes, the \newarray and \readarray part can be moved to another file. But, IMHO, I like the solution from Werner more than mine. – Masroor Jun 28 '14 at 1:10
@Masroor, your code would be helpful if I decide to associate a few quotes with one specific exam. I will save your snippet for future reference. – Mike Taylor Jun 28 '14 at 1:57