Gossamer Forum
Home : General : Perl Programming :

Random Letter string

Quote Reply
Random Letter string
how can I create a string of random letters at a length specified by me? Can somebody please help me? Thanks

I guess I should specify that I want to use both upper and lower case and throw a few numbers 0-9 in there as well.
Thanks again

Quote Reply
Re: Random Letter string In reply to
You can use the following snippet of code to generate a string with two random letters and four random numbers. Of course, you can refine this code to your liking.

Code:
srand();

$randlet1 = chr(int(rand(20)) + 99);
$randlet2 = chr(int(rand(20)) + 99);
$randlet = "$randlet1$randlet2";
$randlet =~ tr/a-z/A-Z/;
$randmax = 9999;
$randnum = int rand $randmax;

$randkey = "$randlet$randnum";
The variable $randmax determines how many random numbers does perl pick from. There is also some documentation at CPAN about making sure a number string has an x amount of numbers to it.

Hope this helps.

Rgds,

Wiliam.

Quote Reply
Re: Random Letter string In reply to
You could use:

my @l=('a'..'z','A'..'Z',0..9);$rstring .=$l[rand 62] for(1 .. $max);

where $max is the maximum number of characters you want it to have.

-- Gordon


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: Random Letter string In reply to
my $number;
$number .= ('A'..'Z', 'a'..'z', 0..9)[rand 62] for (1..25);

Change the (1..25) to be (1..whatever amount of chars you want)

[Edit - Gordo didn't see your response. Thief. Smile]
--mark

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.