Re: what's wrong with this while loop?

From: Date: Tue, 07 Jun 2005 07:47:42 +0000
Subject: Re: what's wrong with this while loop?
References: 1  Groups: php.windows 
Request: Send a blank email to php-windows+get-26075@lists.php.net to get a copy of this message
What's wrong with the while loop ? You mean besides the fact that you are accessing an array and not specifying the index ? $rand_key[] = $key_chars{$rand_pos}; Other than that, saying te result is strange is not good enough. If you want help, you have to be more specific. As to the "50 million question", it depends on the quality of the random number generator, but a fair guess is yes. bo wrote:
Following my code is to write a 15digit random generated password into a file named test.txt. I was trying to keep it random generating 100 passwords and be written into test.txt by using a while loop, however, the result is strange. Take a look at the code (most of it is from php.net) and what's wrong with this while loop? besides, if I generate a large sum of passwords,say 50 million, will it be repetitive? Thank you! <?php /* <meta HTTP-EQUIV="REFRESH" content="1; url=http://svr/rand.php"> */
$length    = 15;
$sum       = 100;
$key_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$rand_max = strlen($key_chars) - 1; $filename = 'test.txt'; $tot=0; while($tot < $sum){ for ($i = 0; $i < $length; $i++){ $rand_pos = rand(0, $rand_max); $rand_key[] = $key_chars{$rand_pos}; } $rand_pass = implode('', $rand_key); $somecontent = "$tot $rand_pass\n"; echo $somecontent; $filename = 'test.txt'; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) {
        echo "Cannot open file ($filename)";
        exit;
} // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) {
      echo "Cannot write to file ($filename)";
      exit;
} echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } $tot++; } ?>


« previous php.windows (#26075) next »