views:

86

answers:

2

Having troubles finding a good way to get a string from a text file (separated by line breaks) randomly.

I want to do a setStringValue:@"random string from file here";

pretty much. Thanks in advance.

A: 

How about you load the whole file (if it is not too big) into an array and then you randomize the index and use that index to get the string from the array?

vodkhang
+1  A: 

Reservoir sampling if you want to avoid loading the complete file into memory at once. For a file just a few lines in length I'd just go with vodkhang's answer, though.

Joey