avoid running out of heap space by sticking our input in a temp file on disk; use zsh RANDOM rather than bsdgames random
if len=0 we probably actually have a single line with no terminating newline, so just cat it