// superfluo in versioni recenti di PHP
srand( (double)microtime() * 1234567 );

codice:
  
$found = false;
while( $found == false ) {  
        $found = &file_exists( ( $filename = rand( 1, 10 ).'.txt' ) ); 
}

echo $filename;