Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Random letters

by alfredo (Novice)
on Apr 12, 2000 at 03:54 UTC ( #7349=perlquestion: print w/replies, xml ) Need Help??
alfredo has asked for the wisdom of the Perl Monks concerning the following question:

I know how to do random numbers, but how do you do random letters? If that can't be done, what would be the best way to assign the random numbers to the letters so they come out randomly.

Looking for "the Uncarved Block." I know you monks have a copy of this lesson.


Replies are listed 'Best First'.
Re: Random letters
by btrott (Parson) on Apr 12, 2000 at 04:09 UTC
    Pretty much the same way you do random numbers, I'd think:
    my @letters = ('a'..'z'); my $random_letter = $letters[int rand @letters];
    If you wanted capitals, as well:
    my @letters = ('a'..'z', 'A'..'Z'); print $letters[int rand @letters], "\n";

      Actually, you don't even need the 'int' - it just slows things down. Faster still, store the number of elements in the array into a scalar:

      @letters=(A..Z,a..z); $total=@letters; $newletter = $letters[rand $total];
Re: Random letters
by plaid (Chaplain) on Apr 12, 2000 at 04:10 UTC
    The easiest way I can see to do it would be taking the random number you get and using it as an index into an array of letters. Something that looks like the following:
    @letters = ('a'..'z'); # or ('A'..'Z') or ('a'..'z', 'A'..'Z'), etc +. $letter = $letters[ int rand scalar @letters ];
    Which could be further condensed down if you only wanted to use this method once and wanted to make your code a little harder to read.
Re: Random letters
by alfredo (Novice) on Apr 12, 2000 at 05:15 UTC

    The answer was so obvious, I should have tried substituting letters for numbers to see if it works. uuurrrggh.

    Thanks. This is part of my first real program in Perl. My only experience is in BasicV and HyperTalk. So this is a new world for me.

RE: Random letters
by Anonymous Monk on Apr 12, 2000 at 04:40 UTC
    chr(random_number + 65); # 65 is ord('A')
      Several comments about this. First of all, you would need to mod random_number by 26 to make sure you get a valid letter (or 52 if you want lowercase too, the lowercase letters follow the capital ones ascii-wise). Secondly, This is not an internationally or long-term safe way of doing it, as assuming 65 is 'A' may break things for someone coming from an international character set, or if (heaven forbid) the ascii table were to change years down the line. In general, something like 'A'..'Z' is safer.
      You *could* do it that way. Here's what I came up with, just to be non-ASCII safe:
      #!/usr/bin/perl -w use strict; my $offset = ord('A'); my $last = 26; if (@_) { $last = 52; } print "Random Character Printer. Press CTRL-D to stop.\n"; do { my $value = int rand(26) + $offset; print $value, "\t", chr($value); } while (<>);
      Note that ASCII values between 91 and 97 inclusive may give you trouble, as A .. Z and a .. z aren't adjacent. You'll want to add a check for that. (Besides, I hadn't used a do-while loop in a while.)
RE: Random letters
by buzzcutbuddha (Chaplain) on Apr 12, 2000 at 17:41 UTC
    foreach $x ('a' .. 'z') {$alph->{$y} = $x; $y++;} foreach (0 .. 25) {$b = rand 25; $alph->{int $b}, "\n";} This will print out 25 random letters from the can change that to print out however many you want. Maurice
      Hmm, you've just reinvented the array. :)

      Seriously, there's no advantage in using a hash with ascending numeric keys corresponding to the letters of the alphabet (especially as you use zero as a base) over a simple array.

      The array is faster and slightly easier to read, too, not to mention much easier to initialize: my @letters = (a .. z);

        Duh...I should have realized that. It seemed fine when I ran it, but looking at the other versions, I see now my mistake! :) Still not bad for a Perl newbie. I did not partake of one of the three virtues: laziness. doh! thanks for the insight. -Maurice

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://7349]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2018-02-23 06:59 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (300 votes). Check out past polls.