1 min read

TIL: How to pick a random line from a file in Ruby without holding it in memory

Picking one line from big files.

I want to pick one random line from a very large file using Ruby but without loading the whole file in memory.

chosen_line = nil
File.foreach('data.txt').each_with_index do |line, number|
  chosen_line = line if rand < 1.0 / (number + 1)
end

Source