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
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