PX : code

random_line by Adam Trachtenberg
Download this code


<?php

# Algorithm comes from Camel book

mt_srand((double) microtime() * 1000000);

function 
random_line($file) {
    
$fp fopen($filer);
    while (++
$i and $line fgets($fp4096)) {
        if ((
mt_rand() / mt_getrandmax() * $i) < 1) { $it $line; }
    }
    
fclose($fp);

    return 
$it;
}


echo 
random_line(/path/to/file.txt);

?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.