Forum Moderators: coopster
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
srand((float)microtime() * 1000000);
shuffle($letters);
$password = $letters[0].$letters[1].$letters[2].$letters[3].$letters[4];
how can i alter this so that each letter does not appear only once? By complete randomness, perhaps 'c' could appear three times, and 'd' could appear twice. What code would allow that to occur?
<?php
srand((float)microtime() * 1000000);
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
while ($password = $letters[0]){
shuffle($letters);
$password = $password . $letters[0];
break 4;
echo $password;
}
?>
what am i doing wrong there?
$passseed = substr(md5(time()),0,6);
$password = "";
for($i=0;$i<=5;$i++) $password .= chr(97+(ord(substr($passseed,$i,1))%7));
You only want the letters 'a' thru 'g', so this method selects characters out of the ASCII table starting at 97 ('a'); and adding the result of the ASCII value of the i'th character of your seed password "MOD 7", which gives a value between 0 and 6, thus selecting a random character between 'a' and 'g', inclusive.
That was based on your original "how could I alter" question; however you could always just do this:
$password = "";for($i=0;$i<=5;$i++) $password .= chr(97+rand(0,6));
...which is just as "random".