My comment would be on the readability and portability of the code.... Think
of someone else coming along to look at that! ;)
This is what I would do; create one function, GenerateString(int $length,
int $type)
function GenerateString($length = 6, $type = 1)
{
$string = '';
// seed rand function
list($usec, $sec) = explode(' ', microtime());
$microtime = ((float)$usec + (float)$sec);
srand((double) $microtime * 1000000);
// loop till $length - 1
for ($i = 0; $i < $length; $i++)
{
// its a password, set the choice to be between 1 and 4
if ($type == 2)
{
$choice = rand(1, 4);
}
// its a username, set the choice to be between 1 and 2
else
{ $choice = rand(1, 2); }
switch ($choice)
{
//generate a random number representing ascii character between a-z
case 1:
case 2: $min = 97; $max = 122; break;
//generate a random number representing ascii character between 0-9
case 3:
case 4: $min = 48; $max = 57; break;
}
// add the character to our string
$string .= chr(rand($min, $max));
}
// return our generated string
return $string;
}
// for readability
define('L_USER', 1);
define('L_PASS', 2);
//for a username, you want 8 character a - z
$username = GenerateString(8, L_USER);
//for a username, you want 6 character a - z and 0-9
$password = GenerateString(6 L_PASS);
Hope that gives you some alternative ideas to explore
> -----Original Message-----
> From: Paul Chvostek [mailto:[EMAIL PROTECTED]]
> Sent: Friday, 20 December 2002 10:47 AM
> To: [EMAIL PROTECTED]
> Subject: [PHP] creating random strings?
>
>
>
> I need to create batches of randomly generated usernames and passwords.
> To start off, I have:
>
> $validuchars='abcdefghijkmnopqrstuvwxyz';
> $validpchars='abcdefghijkmnopqrstuvwxyz23456789';
> $lenu=strlen($validuchars)-1;
> $lenp=strlen($validpchars)-1;
>
> The first method I came up with was:
>
> $uid=''; for($i=8;$i;$i--) $uid.=substr($validchars,mt_rand(0,$lenu),1);
> $pwd=''; for($i=8;$i;$i--) $pwd.=substr($validchars,mt_rand(0,$lenp),1);
>
> But I'm wondering if there's any significant benefit to this instead:
>
> for( $uid=''; strlen($uid)<8;
> $uid.=substr($validuchars,mt_rand(0,$lenu),1) );
> for( $pwd=''; strlen($pwd)<8;
> $pwd.=substr($validpchars,mt_rand(0,$lenp),1) );
>
> I can't see any difference in speed. Does the savings of the $i variable
> have any signficance at all?
>
> Another thing I was thinking of doing was making more pronouncable
> usernames with something like:
>
> $cons="bcdfghjklmnpqrstvwxyz";
> $vowels="aeiouy";
> $lenv=strlen($vowels)-1;
> $lenc=strlen($cons)-1;
> $uid=""; for($i=4;$i;$i--)
> $uid.=substr($cons,mt_rand(0,$lenc),1) .
> substr($vowels,mt_rand(0,$lenv),1);
>
> Any thoughts?
>
> Incidentally, I'm guaranteeing uniqueness of usernames with a unique
> index in the MySQL table that stores this stuff. Including the INSERT,
> I can create about 100000 of these in 60 seconds. So this is more a
> question of style than of practical limitations. ;)
>
> --
> Paul Chvostek <[EMAIL PROTECTED]>
> Operations / Abuse / Whatever +1 416 598-0000
> it.canada - hosting and development http://www.it.ca/
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php