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";
| [reply] [d/l] [select] |
|
@letters=(A..Z,a..z);
$total=@letters;
$newletter = $letters[rand $total];
| [reply] [d/l] |
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. | [reply] [d/l] |
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.
Al
| [reply] |
RE: Random letters
by Anonymous Monk on Apr 12, 2000 at 04:40 UTC
|
chr(random_number + 65); # 65 is ord('A')
| [reply] |
|
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.
| [reply] |
|
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.) | [reply] [d/l] |
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 hash...you can change that to print out however many you want.
Maurice | [reply] |
|
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);
| [reply] [d/l] |
|
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
| [reply] |