Mega Code Archive

 
Categories / Php / Math
 

Finding a random line of a file

<?php function randomint($max = 1) {   $m = 1000000;   return ((mt_rand(1,$m * $max)-1)/$m); } ?> $line_number = 0; $fh = fopen('data.txt','r') or die($php_errormsg); while (! feof($fh)) {     if ($s = fgets($fh)) {         $line_number++;         if (randomint($line_number) < 1) {             $line = $s;         }     } } fclose($fh) or die($php_errormsg); ?>