...

View Full Version : Random code generators!



lansing
06-20-2006, 02:48 AM
I know there are so many ways & versions of code generators that people can through together. I have a code generator that I just copied & pasted a while back, but I don't have a understanding of the break down of the code. Below is my code...I would like for some one to break down what & why each thing is in this function. I am looking to understand how & what each thing does to create the code.

I definitely want to know what is the purpose of having this line in the code: srand((double)microtime()*1000000); & what does the part do that has the % sign in it.

My last question is I need a code to be an exact 10 characters in length...mixed numbers & letters. I tried using ($i == 10), but it wouldn't output a code.


function makeActivationCode()
{
$allow = "abchefghjkmnpqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
$i = 0;
while ($i <= 10)
{
$num = rand() % 33;
$tmp = substr($allow, $num, 1);
$code = $code . $tmp;
$i++;
}
return $code;
}

Thanks!

Digger3000
06-20-2006, 03:16 AM
How about this:


$allow="abchefghjkmnpqrstuvwxyz0123456789";
$code="";
$i=0;
while($i <= 10) {
$rand=rand(1, 36);
$code=$code.$allow[$rand];
$i++;
}
echo $code;

Kid Charming
06-20-2006, 04:51 AM
function makeActivationCode()
{
//string of allowed code characters
$allow = "abchefghjkmnpqrstuvwxyz0123456789";

//seed the upcoming rand() function
//(give it a starting point to run its algorithm from)
srand((double)microtime()*1000000);

//start an 11-iteration loop
//(to get 10, either start $i at 1
//or check '$i < 10'
$i = 0;
while ($i <= 10)
{
//generate a random number,
//divide by 33
//and return the modulus (remainder)
//('%' is the modulus operator)
//this will return a number between 0 & 32
$num = rand() % 33;

//use $num as to determine which character
//from allowed string to use
$tmp = substr($allow, $num, 1);

//concatenate new character to
//to existing code string
$code = $code . $tmp;

//increment loop counter
$i++;
}
return $code;
}

ralph l mayo
06-20-2006, 05:33 AM
Kid Charming covered this pretty well, but here are a few more notes:

srand() hasn't been recommended since php 4.2 so I would probably take it out were I maintaining/repurposing this code. Computerized random numbers, unless you have a cool hardware RNG (and in some cases when you do) are just made by doing repetitive operations on the last generated random value and adjusting the outcome to fall between 0 and 1, typically by just truncating the whole part and leaving the decimals. srand() used to be used to set the first number the operation would be performed on since there is no previous number to jump off from. Now, the rand and mt_rand functions make their own initial "random" value to start off with an srand doesn't do anything useful. I don't know what method PHP uses internally but it's probably more entropic than microtime and hence "better"/"more random."

The % part is modulus. When you divide something, the leftover part is the modulus. It's most often used to alternate between even and odd cases displaying data because n%2 == 0 when n is even (2 goes into n evenly). The modulus in your code seems to be a "clever" way to get the same net effect as the more traditional/understandable/correct multiplication by the desired factor and cast to int.

edit: sorry, I can't resist, even so soon after chastising a complete stranger for using a slightly opaque modulus. Someone please start a good, active programming golf site :(


function makeActivationCode()
{
for($c='';strlen($c)<10;$c.=($x=rand(48,122))<58||$x>96?chr($x):'');return$c;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum