in reply to Random letters

chr(random_number + 65); # 65 is ord('A')

Replies are listed 'Best First'.
RE: RE: Random letters
by plaid (Chaplain) on Apr 12, 2000 at 05:07 UTC
    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.
RE: RE: Random letters
by chromatic (Archbishop) on Apr 12, 2000 at 18:56 UTC
    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.)