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 10 of 10
  1. #1
    Registered User
    Join Date
    Jun 2002
    Location
    Arizona
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    creating a string of random numbers and letters

    I know how to use rand() to create a random number but how can i create a string that is 7 charactors that is a mixture of numbers and letters. Thanks.

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know if there is a function that can do this. You could possibly use two random number generators, one to chose between an array of letters or numbers and the second to either choose which letter (position) in the array or a random number. That is the only way that comes to me right now...off the top of my head....


    Jason

  • #3
    Registered User
    Join Date
    Jun 2002
    Location
    Arizona
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so you can do rand(a,z)?

  • #4
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    not that I know of....do a random number from 1-2 that will chose whether you are putting in your 7element array or whatever a number or letter....then the next one would be say for letter: a number that is from 1-26 for each letter of the alphabet and then for number: would be a random number....and loop 7 times....does that make sense?


    Jason

  • #5
    Registered User
    Join Date
    Jun 2002
    Location
    Arizona
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    okay this is what i made..

    <?php

    $v = 1;
    while($v <= 7)
    {
    $randnumberagain = rand(1,4);
    if($randnumberagain%2)
    {
    $randome_alph = rand(1,26);

    if($randome_alph == 1){$add = "a";}
    if($randome_alph == 2){$add = "b";}
    if($randome_alph == 3){$add = "c";}
    if($randome_alph == 4){$add = "d";}
    if($randome_alph == 5){$add = "e";}
    if($randome_alph == 6){$add = "f";}
    if($randome_alph == 7){$add = "g";}
    if($randome_alph == 8){$add = "h";}
    if($randome_alph == 9){$add = "i";}
    if($randome_alph == 10){$add = "j";}
    if($randome_alph == 11){$add = "k";}
    if($randome_alph == 12){$add = "l";}
    if($randome_alph == 13){$add = "m";}
    if($randome_alph == 14){$add = "n";}
    if($randome_alph == 15){$add = "o";}
    if($randome_alph == 16){$add = "p";}
    if($randome_alph == 17){$add = "q";}
    if($randome_alph == 18){$add = "r";}
    if($randome_alph == 19){$add = "s";}
    if($randome_alph == 20){$add = "t";}
    if($randome_alph == 21){$add = "u";}
    if($randome_alph == 22){$add = "v";}
    if($randome_alph == 23){$add = "w";}
    if($randome_alph == 24){$add = "x";}
    if($randome_alph == 25){$add = "y";}
    if($randome_alph == 26){$add = "z";}

    }
    else
    {
    $add = rand(1,9);
    }
    $randstr = $randstr;
    $randstr .= $add;
    $v++;
    }
    $randstr = strtoupper($randstr);
    echo $randstr;
    ?>

  • #6
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $randstr[7];
    for ($i =0; $i < 7; $i++){
    $j = rand(1,2);
    if ($j ==1){ //alphabet
    $alpha = {'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'}
    $k = ran(0,25);
    $randstr[$i]=$alpha;
    }
    else { //numbers
    $randstr[$i] = $ran(0,9);
    }
    }


    thats an ugly way...didn't test this but you get the idea....



    Jason

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    uniqid() creates a random string... its mostly numeric but you can manipulate that somewhat , the last version here produces an alpha-numeric mixed case unique string pretty good for password generation etc, grabbing the last 7 characters gives better values for whatever reason (I assume because uniqid works on the current timestamp?)

    PHP Code:
    <?
    echo substr(uniqid(""),-7).'<br />';
    echo 
    substr(md5(uniqid("")),-7).'<br />';
    echo 
    substr(base64_encode(md5(uniqid(""))),-7);
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think:

    PHP Code:

    $str 
    '';

    for(
    $i 0$i 7$i++) 
    {
          
    $str .= chr(rand(ord("a"),ord("Z")));

    Hope this helps...

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $str = substr(md5(rand(0,99999)),0,7);

    seems fastest. might need to salt the rand if your build is oldish though.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #10
    Registered User
    Join Date
    Jun 2002
    Location
    Arizona
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks much


  •  

    Posting Permissions

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