SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: random alphabet

  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    random alphabet

    Code:
    $randomNumber=rand(0,9);
    The code above produces a random number from 0 to 9.

    How can I get $randomAlphabet from a to z?

    The code below seems not to work.
    Code:
    $randomAlphabet=rand(a,z);

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) put the letters in an array

    2) generate random number between 0 - 25

    3) get the array element value whose index is that from 2)

    or depending on what you want to do, use shuffle

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by dotJoon View Post
    The code below seems not to work.
    Code:
    $randomAlphabet=rand(a,z);
    If you'd read the manual, you would've know that.
    rand — Generate a random integer
    You could write a function that gets a random integer from 1 to 26, and then take the corresponding letter.

    Edit: kalon's 0-25 is better, because arrays start with key value 0.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    If only there were a way to get from an integer to a character...

    Wait. Don't we have this thing called the ASCII table?

    PHP Code:
    chr(rand(97,122)) 
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like taking the scenic route

    and the ascii values for lower and upper case letters have a gap between them.

    uppercase is 65 -90

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2010
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Strings are small integers. Everything related to programming is based on numbers. Also, I'm amazed by the number of the topics started by dotJoon, all of these questions asked lately are easily found at google seeing they've been answered millions of times.

  7. #7
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     
    <?php
    
    $letters = array_merge(range('a','z'),range('A','Z'));
     
    echo $letters[rand(0,51)];
     
    ?>

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    chr(rand() > 0.5 rand(6590) : rand(97,122)) 
    I'm really not a fan of creating arrays when solutions like the above are available ...
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a wise person once told me

    There's more than one way to skin a cat.
    in this case I would use an array because if I forgot to document in comments what chars the integers represented, someone else, or even me, looking at the code in the future might have to go look them up.

    creating the array makes it a lot easier for me to see what is going on in the code without running it or looking anything up.

  10. #10
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    In order to get random alphabet from a to c, I wrote the code below
    Code:
    <?php
    $alphabet=array("a","b","c");
    
    echo $alphabet[0];
    echo $alphabet[1];
    echo $alphabet[2];
    
    $random_number=rand(0,2);
    
    echo $random_number ;
    
    $random_alphabet=$alphabet['$random_number'];
    
    echo $random_alphabet;
    ?>
    But the code above doesn't work correctly. I guess the code in read has some problem.

    How can I fix the problem?

  11. #11
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dotJoon View Post
    In order to get random alphabet from a to c, I wrote the code below
    Code:
    <?php
    $alphabet=array("a","b","c");
    
    echo $alphabet[0];
    echo $alphabet[1];
    echo $alphabet[2];
    
    $random_number=rand(0,2);
    
    echo $random_number ;
    
    $random_alphabet=$alphabet['$random_number'];
    
    echo $random_alphabet;
    ?>
    But the code above doesn't work correctly. I guess the code in read has some problem.

    How can I fix the problem?
    You enclosed the $random_number call in single quotes - So it's looking for the value in the array that corresponds to $randon_number I believe, just remember when you're referencing the position of something in an array through an integer, don't use quotes.

    So

    PHP Code:
    $random_alphabet=$alphabet[$random_number]; 
    Works perfect.

  12. #12
    SitePoint Zealot
    Join Date
    Jun 2010
    Location
    Arizona
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How random does it need to be? rand() and mt_rand() are pseudo-random. That is, they fake it and are not qualified as cryptographically secure (i.e. someone can get the sequence if they guess the right seed). If you need truly random numbers, use random.org.
    Thomas Hruska

    Single Sign-On Server/Client - The PHP login system that rocks.


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
  •