Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Function To Generate Random String

  1. #1
    Senior Member ksg91's Avatar
    Join Date
    Jan 2010
    Posts
    164
    Thanks
    9
    Thanked 24 Times in 15 Posts
    Blog Entries
    1
    Rep Power
    4

    Default Function To Generate Random String

    I was working on Url Shortner script and I needed a better algorithm that can generate a fixed length random string which have very less probability of collision with other strings in db. So I created one simple, yet efficient algorithm. I hope this is good. It returns you 8 character long random string.
    PHP Code:
    <?php
    //////////////////////////////////////
    // Author: Kishan Gor a.k.a Ksg91
    // Date: 27-12-2010
    /////////////////////////////////////

    //returns 8 character long random string
    function getKSGRandomString()
    {
      
    $a=array("0"=>"0","1"=>"1","2"=>"2","3"=>"3","4"=>"4","5"=>"5","6"=>"6","7"=>"7","8"=>"8","9"=>"9","10"=>"a","11"=>"b","12"=>"c","13"=>"d","14"=>"e","15"=>"f","16"=>"g","17"=>"h","18"=>"i","19"=>"j","20"=>"k","21"=>"l","22"=>"m","23"=>"n","24"=>"o","25"=>"p","26"=>"q","27"=>"r","28"=>"s","29"=>"t","30"=>"u","31"=>"v","32"=>"w","33"=>"x","34"=>"y","35"=>"z","36"=>"A","37"=>"B","38"=>"C","39"=>"D","40"=>"E","41"=>"F","42"=>"G","43"=>"H","44"=>"I","45"=>"J","46"=>"K","47"=>"L","48"=>"M","49"=>"N","50"=>"O","51"=>"P","52"=>"Q","53"=>"R","54"=>"S","55"=>"T","56"=>"U","57"=>"V","58"=>"W","59"=>"X","60"=>"Y","61"=>"Z");
      
    $rand1=mt_rand(00000000,99999999);
      
    $rand2=mt_rand(00000000,99999999);
      
    $string='';
      while(
    $rand1>0)
      {
        
    $x $rand1 100;
        
    $rand1=floor($rand1/100);
        
    $x $x 60
        
    $string.=$a[$x];
      }
      while(
    $rand2>0)
      {
        
    $x $rand2 100;
        
    $rand2=floor($rand2/100);
        
    $x $x 60
        
    $string.=$a[$x];
      }
      return 
    $string;
    }
    ?>
    Expecting your advices and suggestions to improve it more.

  2. #2
    Senior Member frostymarvelous's Avatar
    Join Date
    Jun 2010
    Location
    Accra, Ghana.
    Posts
    258
    Thanks
    3
    Thanked 20 Times in 17 Posts
    Rep Power
    3

    Default

    Won't this take too much time?
    I would have done something like this.

    $x= 0;
    $string= '';
    While($x <= $len )
    {
    $v= rand(0,64);
    $string .= $a[$v];
    $x ++;
    }

    Nothing fancy.
    I'm mobile so pardon any errors, I'm just typing straight. And yes I know a for loop would be better. Hehe.

  3. #3
    Senior Member
    Join Date
    Mar 2010
    Location
    Ghana
    Posts
    128
    Thanks
    0
    Thanked 27 Times in 17 Posts
    Rep Power
    4

    Default

    Well i guess frost is correct but i will prefer this
    <?php
    $rand1=rand(0,999);
    $rand2=rand(0,999);
    $rand3=rand(1,10);
    $rand_string=pow($rand1,$rand2);
    $rand_string=substr($rand_string,1,$rand3);
    sorry on mobile pardon me 4 the errors.

  4. #4
    Super Moderator subzero's Avatar
    Join Date
    Mar 2006
    Location
    Your Screen.
    Posts
    3,971
    Thanks
    442
    Thanked 379 Times in 180 Posts
    Blog Entries
    7
    Rep Power
    0

    Default

    lol

    foreach('$a as $a');
    Visit: [Only registered and activated users can see links. Click Here To Register...]
    Visit: [Only registered and activated users can see links. Click Here To Register...]
    _______
    SCRIPTS FOR SALE BY SUBZERO
    Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders.
    FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others...
    _______
    Info & Tips
    php.net
    w3schools.com

  5. #5
    Senior Member frostymarvelous's Avatar
    Join Date
    Jun 2010
    Location
    Accra, Ghana.
    Posts
    258
    Thanks
    3
    Thanked 20 Times in 17 Posts
    Rep Power
    3

    Default

    I just noticed you tagged it url shortener, so performance won't be an issue to you.
    Rather, the uniqueness is important. Cool.

  6. #6
    Moderator Anshul's Avatar
    Join Date
    Jun 2007
    Location
    India
    Posts
    296
    Thanks
    5
    Thanked 19 Times in 15 Posts
    Rep Power
    0

    Default

    you may try using mcrypt based encryption

  7. #7
    Super Moderator metulj's Avatar
    Join Date
    Jan 2007
    Location
    your root... now think fast... =]
    Posts
    1,711
    Thanks
    558
    Thanked 212 Times in 130 Posts
    Rep Power
    0

    Default

    does it have to be random string?
    is there any advantage of randomizing?

    i mean, currently im also working
    on URL shortener script...
    but basicly i dont mind if it starts like

    • domaine.com/1
    • domaine.com/2
    • etc, etc
    • ...........
    • domaine.com/1359
    • etc...

    im also trying to add option to create manual URL..
    for example if i have some tralala.zip to link...
    it'll be domaine.com/tralala.zip <<== ofcourse if that name still available I.E. not yet in DB
    It's better to keep your mouth shut and give the impression that you're stupid, than to open it and remove all doubt.
    ⓣⓗⓔ ⓠⓤⓘⓔⓣⓔⓡ ⓨⓞⓤ ⓑⓔ©ⓞⓜⓔ, ⓣⓗⓔ ⓜⓞⓡⓔ ⓨⓞⓤ ⓐⓡⓔ ⓐⓑⓛⓔ ⓣⓞ ⓗⓔⓐⓡ !
    ιη тнєσяу, тнє ρяα¢тι¢є ιѕ α яєѕυℓт σƒ тнє тнєσяу, вυт ιη ρяα¢тι¢є ιѕ тнє σρρσѕιтє.
    キノgんイノ刀g 4 ア乇ムc乇 ノ丂 レノズ乇 キucズノ刀g 4 √ノ尺gノ刀ノイリ!

  8. #8
    Senior Member frostymarvelous's Avatar
    Join Date
    Jun 2010
    Location
    Accra, Ghana.
    Posts
    258
    Thanks
    3
    Thanked 20 Times in 17 Posts
    Rep Power
    3

    Default

    I don't think there's anything wrong with randomising but you should always check the Db to ensure it is unique.

    Do something like yourls. Start with single char and then progress.

  9. #9
    Senior Member ksg91's Avatar
    Join Date
    Jan 2010
    Posts
    164
    Thanks
    9
    Thanked 24 Times in 15 Posts
    Blog Entries
    1
    Rep Power
    4

    Default

    Quote Originally Posted by frostymarvelous [Only registered and activated users can see links. Click Here To Register...]
    Won't this take too much time?
    I would have done something like this.

    $x= 0;
    $string= '';
    While($x <= $len )
    {
    $v= rand(0,64);
    $string .= $a[$v];
    $x ++;
    }

    Nothing fancy.
    I'm mobile so pardon any errors, I'm just typing straight. And yes I know a for loop would be better. Hehe.
    actually, my algorithm was to convert an integer string into random character string. I was actually going to pass integer and get its string but then created like that. You have bit faster implementation of above function but algorithm is that get 16 digit integet, make pair of two integer and take modulo 60 (because we have 61 character in above array) and append it to string.
    Quote Originally Posted by razzbee [Only registered and activated users can see links. Click Here To Register...]
    Well i guess frost is correct but i will prefer this
    <?php
    $rand1=rand(0,999);
    $rand2=rand(0,999);
    $rand3=rand(1,10);
    $rand_string=pow($rand1,$rand2);
    $rand_string=substr($rand_string,1,$rand3);
    sorry on mobile pardon me 4 the errors.
    can you explain this code?
    Quote Originally Posted by subzero [Only registered and activated users can see links. Click Here To Register...]
    lol

    foreach('$a as $a');
    Didnt got what you wanted to post.
    Quote Originally Posted by Anshul [Only registered and activated users can see links. Click Here To Register...]
    you may try using mcrypt based encryption
    thanks. will check about that.
    Quote Originally Posted by metulj [Only registered and activated users can see links. Click Here To Register...]
    does it have to be random string?
    is there any advantage of randomizing?

    i mean, currently im also working
    on URL shortener script...
    but basicly i dont mind if it starts like

    • domaine.com/1
    • domaine.com/2
    • etc, etc
    • ...........
    • domaine.com/1359
    • etc...

    im also trying to add option to create manual URL..
    for example if i have some tralala.zip to link...
    it'll be domaine.com/tralala.zip <<== ofcourse if that name still available I.E. not yet in DB
    Randomizing string has advantage of less guess/hit ratio. If its in sequence, one can guess any valid link. And some people wont like sharing their shorted link with everyone.
    And I am making it very basic, just wanted to make it for personal use but not limited to me only.

  10. #10
    Senior Member frostymarvelous's Avatar
    Join Date
    Jun 2010
    Location
    Accra, Ghana.
    Posts
    258
    Thanks
    3
    Thanked 20 Times in 17 Posts
    Rep Power
    3

    Default

    I can't quote the whole post and I'm mobile but in reply to the reply you made to me.
    Wow. That must have involved some thinking right there. Mine was just off the bat.
    I. Saving this for future reference. Never knlw when it will come in handy. Either for use or for study.

    Btw. I studied oop just by looking at ksg's code. He's that good.

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Generate SEO / Friendly URLs
    By morency in forum Snippets, Functions and Classes
    Replies: 0
    Last Post: 07-09-10, 19:56
  2. generate random password/random string
    By GumSlone in forum Snippets, Functions and Classes
    Replies: 1
    Last Post: 18-07-10, 19:18
  3. Generate random passwords with PHP
    By riderz in forum Tutorials
    Replies: 0
    Last Post: 20-02-10, 20:28
  4. Replies: 1
    Last Post: 26-05-09, 20:34
  5. Generate A Password
    By WhiteWarrior in forum Coding Forum
    Replies: 3
    Last Post: 24-01-08, 18:08

Tags for this Thread

Bookmarks

Posting Permissions

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

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19