I can’t think of a way to randomly execute the entire file without any kind of saving a list of what has already been written. I think that if I had to do an efficient memory shuffling, I would scan the file by building a list of offsets for new lines. As soon as I have a list of new line offsets, I would randomly select one of them, write it to stdout and remove it from the list of offsets.
I am not familiar with perl or python, but can demonstrate with php.
<?php
$offsets = array();
$f = fopen("file.txt", "r");
$offsets[] = ftell($f);
while (! feof($f))
{
if (fgetc($f) == "\n") $offsets[] = ftell($f);
}
shuffle($offsets);
foreach ($offsets as $offset)
{
fseek($f, $offset);
echo fgets($f);
}
fclose($f);
?>
, , , ( ):
- , stdout
- Loop bytes_written == filesize
- ,
- ,
- ,
- 3.