1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

I need help generating random strings

Discussion in 'PHP' started by ChristopherLeeMaher.Com, Sep 21, 2011.

  1. #1
    I need to generate 3 random strings.
    1) A random string that's 250 charters, the random string needs to have uppercase, lowercase, numbers.
    2) A random string that's 200 charters, the random string needs to have uppercase, lowercase, numbers.
    3) A random string that's 150 charters, the random string needs to have uppercase, lowercase, numbers.

    Thanks,
    Chris
     
    Solved! View solution.
    ChristopherLeeMaher.Com, Sep 21, 2011 IP
  2. livedating

    livedating Active Member

    Messages:
    161
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    83
    #2
    
    <?
    function rand_str1($len) {      // random string
            $str = '';
            for ($i=0; $i<26; $i++) $str .= chr(ord('A')+$i).chr(ord('a')+$i);
            for ($i=0; $i<10; $i++) $str .= $i;
            $ret = '';
            for ($i=0; $i<$len; $i++) $ret .= substr($str, rand(0, strlen($str)-1), 1);
            return $ret;
    }
    
    function rand_str2($len) {      // simple but only lowercase
            $ret = '';
            while (strlen($ret)<$len) $ret .= md5(rand());
            return substr($ret, 0, $len);
    }
    
    echo rand_str1(200);
    echo "<BR>";
    echo rand_str2(200);
    ?>
    
    Code (markup):
    You can use any of two functions above. The 2nd one is simple but only gives lowercase strings, and the 1st one gives upper and lower strings. Both add numeric chars.
    Does it suite your needs?
     
    livedating, Sep 22, 2011 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    Same thing as @livedatesearch, just a little different:
    
    <?php 
    
    function genRandomString($length) {
        $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUBWXYZ";
        $string = "";    
        for ($p = 0; $p < $length; $p++) {
            $string .= $characters[mt_rand(0, strlen($characters))];
        }
        return $string;
    }
    
    $string1 = genRandomString(250); 
    $string2 = genRandomString(200); 
    $string3 = genRandomString(150); 
    
    echo $string1; // 2 or 3 where needed.
    
    ?>
    
    
    PHP:
     
    MyVodaFone, Sep 22, 2011 IP
  4. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #4
    try this..
    
    
    <?php
    
    $characters = 250;
    $letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
    
    $output = '';
    for($i=1; $i<=$characters; $i++)
    {
    	switch(rand(0,1))
    	{
    		case 0:
    			$output.= rand(0,9);
    			break;
    		case 1:
    			$letter = $letters[array_rand($letters)];
    			$output.= rand(0,1)?strtoupper($letter):$letter;
    			break;
    	}
    }
    
    echo $output;
    
    ?>
    
    
    PHP:
    just change the $characters to change the number of characters needed..
     
    JohnnySchultz, Sep 22, 2011 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    Whatever function you use, use mt_rand instead of rand. http://php.net/manual/en/function.mt-rand.php

    Also, it's not really probably, but you could theoretically have colissions with this script. If you need something random and unique, make a hash based on a timestamp or something that doesn't repeat.

    This is one, I use for generating random strings.

    
    function random_string($length=16) {
            
            $string = '';
            $possible = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
            for ($i = 1; $i < $length; $i++) {
                $char = substr($possible, mt_rand(0, strlen($possible) - 1), 1);
                $string .= $char;
            }
    
            return $string;
        }
    
    echo random_string(250);
    echo random_string(200);
    echo random_string(150);
    
    
    PHP:
     
    jestep, Sep 22, 2011 IP
    jevchance likes this.
  6. jevchance

    jevchance Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    0
    #6
    I agree with jestep, its good to incorporate the time to avoid any chance of duplicates.
     
    jevchance, Sep 22, 2011 IP
  7. #7
    Avoiding for loop in here, works almost same way, but expected to be a bit quick.

    
    <?php
    
        function random_string($length)
        {
            $possible = str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
    
            while(strlen($possible) < $length)
            {
                $possible .= str_shuffle($possible);
            }
            return substr(str_shuffle($possible), 0, $length);
        }
        
    	echo random_string(250)."\n";
    	echo random_string(200)."\n";
    	echo random_string(150)."\n";
    		
    ?>
    PHP:
    Hope it helps.
     
    Vooler, Sep 23, 2011 IP