Thread: Srand problem?

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    5

    Srand problem?


    This code has worked for ages so am not sure what is wrong now.
    Fatal error: Call to undefined function 1352483998() line 31
    Line 31 is the srand line:

    Code:
    //function
    function generatePassword($length=6,$level=2){
    
       list($usec, $sec) = explode(' ', microtime());
       srand((float) $sec   ((float) $usec * 100000));
    
       $validchars[1] = "23456789abcdefghjkmnpqrstuvwxyz";
       $validchars[2] = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
       $validchars[3] = "23456789_!@#$%&*()-= /abcdefghjkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ_!@#$%&*()-= /";
    
       $password  = "";
       $counter   = 0;
    
       while ($counter < $length) {
         $actChar = substr($validchars[$level], rand(0, strlen($validchars[$level])-1), 1);
    
         // All character must be different
         if (!strstr($password, $actChar)) {
            $password .= $actChar;
            $counter  ;
         }
       }
    
       return $password;
    
    }
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,997
    Rep Power
    9397
    PHP Code:
    srand((float) $sec   ((float) $usec 100000)); 
    You're missing an operator in that big empty space there.

    Also,
    1. microtime(true) will give you a floating-point number rather than the stupid string it gives by default.
    2. You don't need to seed the RNG - PHP does it for you.
    3. Use mt_rand().
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    5
    Originally Posted by requinix
    PHP Code:
    srand((float) $sec   ((float) $usec 100000)); 
    You're missing an operator in that big empty space there.

    Also,
    1. microtime(true) will give you a floating-point number rather than the stupid string it gives by default.
    2. You don't need to seed the RNG - PHP does it for you.
    3. Use mt_rand().
    the same code on a different server works yet all of a sudden on this server it doesn't?!

    Code:
    //function
    function generatePassword($length=6,$level=2){
    
       list($usec, $sec) = explode(' ', microtime());
       srand((float) $sec   ((float) $usec * 100000));
    
       $validchars[1] = "23456789abcdefghjkmnpqrstuvwxyz";
       $validchars[2] = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
       $validchars[3] = "23456789_!@#$%&*()-= /abcdefghjkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ_!@#$%&*()-= /";
    
       $password  = "";
       $counter   = 0;
    
       while ($counter < $length) {
         $actChar = substr($validchars[$level], rand(0, strlen($validchars[$level])-1), 1);
    
         // All character must be different
         if (!strstr($password, $actChar)) {
            $password .= $actChar;
            $counter  ;
         }
       }
    
       return $password;
    
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,399
    Rep Power
    594
    You probably inadvertently deleted an operator. Most likely you want:
    PHP Code:
    srand((float) $sec + ((float) $usec 100000)); 
    P.S. Note the use of [ PHP ] tags rather than [ CODE ] tags.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    5
    will this work?
    where is it being seeded?

    Code:
    //function
    function generatePassword($length=6,$level=2){
    
       list($usec, $sec) = explode(' ', microtime(true));
       mtrand((float) $sec   ((float) $usec * 100000));
    
       $validchars[1] = "23456789abcdefghjkmnpqrstuvwxyz";
       $validchars[2] = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
       $validchars[3] = "23456789_!@#$%&*()-= /abcdefghjkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ_!@#$%&*()-= /";
    
       $password  = "";
       $counter   = 0;
    
       while ($counter < $length) {
         $actChar = substr($validchars[$level], rand(0, strlen($validchars[$level])-1), 1);
    
         // All character must be different
         if (!strstr($password, $actChar)) {
            $password .= $actChar;
            $counter  ;
         }
       }
    
       return $password;
    
    }
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    5
    Originally Posted by gw1500se
    You probably inadvertently deleted an operator. Most likely you want:
    PHP Code:
    srand((float) $sec + ((float) $usec 100000)); 
    P.S. Note the use of [ PHP ] tags rather than [ CODE ] tags.
    just added the operator and get the same error.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    5
    The error was my host's web editor saves the files and strips operators...at least strips the + operator from all code!!!!!

    I added the + operator and now the script times out here:

    while ($counter < $length) { $actChar = substr($validchars[$level], rand(0, strlen($validchars[$level])-1), 1);
    Last edited by qwertyjjj; November 9th, 2012 at 12:35 PM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,399
    Rep Power
    594
    Cross posting is frowned upon and in some cases will end all help.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    5
    Originally Posted by gw1500se
    Cross posting is frowned upon and in some cases will end all help.
    I didn't. The site I copied from has some javascript to auotmatically add a cross post when you do ctrl+c

    The error was my host's web editor saves the files and strips operators...at least strips the + operator from all code!!!!!

    I added the + operator and now the script times out here:

    while ($counter < $length) { $actChar = substr($validchars[$level], rand(0, strlen($validchars[$level])-1), 1);
  18. #10
  19. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,867
    Rep Power
    6351
    You DID cross-post. Cross-posting means posting across multiple websites. It's against the rules because it's a waste of our time if there's already someone saying what we're saying on another website.

    Speaking of a waste of time, you know srand() isn't even necessary, right? It stopped doing anything useful in PHP 4.2, released more than 10 years ago.

    As a final note, if you're using an editor which strips anything from your files, especially basic math operators, stop using it immediately.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    5
    The error was my host's web editor saves the files and strips operators...at least strips the + operator from all code!!!!!

    It stripped out the $counter++ as well!
  22. #12
  23. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,867
    Rep Power
    6351
    Use a new host. They're incapable of accepting computer code.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo