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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    Real mystery with loop results

    PHP Code:
    self::$_STRLST 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    // preceding code
      
    private function strGen($_len
                   {
                    
    $this->len $_len;
                    
    $this->strLst self::$_STRLST;
                    
    $this->out ='';
                    
    $this->outEx '';
                    
    $this->itr 0;
                    
    //while(strlen($this->out) < $this->len)
                    
    for($this->itr$this->itr $this->len$this->itr++)
                       {
                        
    $this->selection rand(0count($this->strLst) - );
                        
    $this->candidate $this->strLst[$this->selection];
                        
    $this->out .= $this->candidate;
                        
    $this->outEx .= ' '.$this->candidate;
                       }
                    return 
    ltrim($this->outEx).'||'.$this->out// for rendering
                   
    }
    //....
    print str_replace('||''<br />'$_CAP->getVal(5)); // getVal calls strGen(5) 
    samples of output:
    f N W Z Y
    fNWZY 9 9 (9 9??)

    K v 0 5 R
    Kv05R

    r q v z e
    rqvze 7 (7 ?)

    S f Y L Q
    SfYLQ c (c ?)

    Sometimes the strings have matching character sets.
    Most time the lower one has as many as 3 extra characters that
    are most often numbers, but can be letter characters.
    Where are the extra chars coming from?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    What else does getVal do? The extra characters are possibly (probably?) coming from there.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post
    from comment in source:
    // getVal calls strGen(5)
    I coded getVal just for testing purposes. It will produce
    the values of various critical private vars according to option
    argument passed to it. In this case,
    the output of strGen(5) is the output of getVal(5)

    That is why it is a mystery. I can try a test case with code that is not
    wrapped in class/object code.

    self::$_STRLST is actually an array of individual chars. That is why I am using count() instead of strlen()
    Last edited by anotherJEK; 02-07-2013 at 07:53 PM.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    here's a bit more

    PHP Code:
    function strGen($_len
                   {
                   
    $_charStr 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
                    
    $len $_len;
                    
    $strLst str_split($_charStr);
                    
    $out ='';
                    
    $outEx '';
                    
    $itr 0;
                    
    //while(strlen($this->out) < $this->len)
                    
    for($itr$itr $len$itr++)
                       {
                        
    $selection .= '-'.rand(0count($strLst) );
                        
    $candidate $strLst[$selection];
                        
    $out .= $candidate;
                        
    $outEx .= $candidate//' '.
                       
    }
                    return 
    $selection//$outEx.'--'.$out; // for rendering
                   
    }
                   
    print 
    str_replace('-''<br />'strGen(5)); 
    output samples:

    2
    11
    59
    59
    26 2

    0
    43
    10
    3
    17 4 4

    5
    61
    43
    51
    22 b <- extra letter??

    13
    10
    33
    51
    45

    62
    26
    32
    49
    23 3 3 3

    this appears to be a bug with rand()... unless I am not using it right.
    If anyone monitoring this forum or posssibly, the moderator has connects
    to php developers, this might be brought up. I know they are sensitive to
    claims of bugs. I have not been able to re subscribe to any php mailing lists

    PHP Version 5.2.0-8+etch5~pu1
    Linux AMDAM2DEB 2.6.18-5-amd64 #1 SMP Thu May 31 23:51:05 UTC 2007 x86_64
    Build Date: May 23 2007 20:06:20
    Apache 2.0 Handler
    PHP API 20041225
    PHP Extension 20060613
    Zend Extension 220060519

    Client: Firefox 3x running on MacOSX 10.5x

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    Still more:

    PHP Code:
    $_code explode('||'$_CAP->getVal(5));
       print 
    'short str: '.md5($_code[0])."<br />long str: ".md5($_code[1]);
       if(
    md5($_code[0]) == md5($_code[1]))
         {
          print 
    "<br />equal";
         }
       else
         {
          print 
    'There is a functional difference in the strings';
         } 
    Sample output:
    short str: 5c79705ba8c52b69619b8b3ea8dac29c
    long str: 5c79705ba8c52b69619b8b3ea8dac29c
    equal b b b b <- notice the extra chars still showing up

    short str: 0adfc3f4c173b7754bb436fb203470e5
    long str: 0adfc3f4c173b7754bb436fb203470e5
    equal 7 <- notice the extra chars still showing up

    short str: f47f00ff4f6cf8ca8a57aef9c566ca3e
    long str: f47f00ff4f6cf8ca8a57aef9c566ca3e
    equal

    These samples from reverting back to original code with revision to eliminate the spaces:
    PHP Code:

    // preceding code is the same as the original post version
                   
    for($this->itr$this->itr $this->len$this->itr++)
                       {
                        
    $this->selection rand(0count($this->strLst) - );
                        
    $this->candidate $this->strLst[$this->selection];
                        
    $this->outEx .= $this->candidate;
                        
    $this->out .= $this->candidate// NO SPACES ADDED
                       
    }
                    return 
    $this->outEx."||".$this->out;
    // etc.... 
    This is very strange: calling md5() on the strings results in equal hashes
    which means that inspite of the extra apparent chars, the strings are in
    fact equal.

    **** the phantom chars disappear when I use 8 as the string length argument *****
    Last edited by anotherJEK; 02-09-2013 at 06:24 AM. Reason: punch line


  •  

    Posting Permissions

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