Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    346
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Random code generators!

    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.

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

    Thanks!

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    Texas
    Posts
    472
    Thanks
    1
    Thanked 0 Times in 0 Posts
    How about this:

    PHP Code:
    $allow="abchefghjkmnpqrstuvwxyz0123456789";
    $code="";
    $i=0;
    while(
    $i <= 10) {
    $rand=rand(136);
    $code=$code.$allow[$rand];
    $i++;
    }
    echo 
    $code
    If you're reading this, it may already be too late!

  • #3
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    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$num1);

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

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


  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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
    PHP Code:
    function makeActivationCode()
    {
        for(
    $c='';strlen($c)<10;$c.=($x=rand(48,122))<58||$x>96?chr($x):'');return$c;

    Last edited by ralph l mayo; 06-20-2006 at 05:35 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •