Strictly speaking, you can never read them randomly.
Wait! Before getting angry and down-voting, read thoroughly!
It depends what you call "read" and "randomly". Let's assume that you're allowed to each line only once (no pre-reading; if you allow pre-reading, the problem is trivial: read whole file into collection of lines and feed the lined at random
). Let's assume "randomly" means that random number generator generates a line number, then the algorithm reads and return the line of this number.
There is a well-known terminology: random access (like in RAM -- Random Access Memory).
If you mean something else, please skip the rest of my post and explain what exactly is require -- I'm not responsible for answering wrong question only because of your incomplete specification.
So, under these assumption, this is not possible with text file and lines where lines are separated by end of line characters. It is only possible (and trivial) if the file consist of fixed-size records, more exactly, if the file positions of each record boundaries are known in advance. Here is why:
Arbitrary text file is composed of lines of different length; lines are separated by some end-of-line characters which positions are unknown. In other words, there is no descriptor of the line lengths/positions which you could read at the end of the file. If you read sequentially, you discover where the lines are as you read. You can read randomly, but you go the the line you need to know its position -- and it is never known before you read everything before
. Reading the content before a given line is equivalent to sequential reading => your reading is not random.