Getting a random line from a file – and a question!!!

29 10 2007


line = IO.readlines('some_file')
c = rand*line.length.to_i
puts line[c-1]

Very simple logic and it can be easily understood by just looking at the code itself. No explanation needed. If someone knows a better way to do it then please post it in the comments. BTW..thats in ruby.








Follow

Get every new post delivered to your Inbox.