CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to generate a word with 8 random characters?

 



Rivotti
User

Apr 6, 2000, 3:15 PM

Post #1 of 11 (1944 views)
How to generate a word with 8 random characters? Can't Post

Hi:

How do I generate a word with 8 random characters? Something like: Ejy2zZcd.

Thanks
Rivotti


Jasmine
Administrator / Moderator

Apr 6, 2000, 3:51 PM

Post #2 of 11 (1944 views)
Re: How to generate a word with 8 random characters? [In reply to] Can't Post

I'm certain there are other ways to do this, but here's what I'm thinking of right now )...

First, build a list of valid characters to be included, then create a loop that loops through the number of characters you want to have, appending one random character at a time.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@chars=("A".."Z","a".."z","0".."9");
for ($i=1; $i<=8;$i++){
$random .= $salt[rand @chars];
}
</pre><HR></BLOCKQUOTE>


Rivotti
User

Apr 10, 2000, 3:15 AM

Post #3 of 11 (1944 views)
Re: How to generate a word with 8 random characters? [In reply to] Can't Post

Hi Jasmine:

I've tried what you told me to do but nothing happens. I looked in to perlfaq4, in "How do I select a random element from an array?", and the code is the same except for using strand at the top of the program. Still it doesn't work. I'm I doing something wrong?

Thanks
Rivotti


Jasmine
Administrator / Moderator

Apr 10, 2000, 8:45 AM

Post #4 of 11 (1944 views)
Re: How to generate a word with 8 random characters? [In reply to] Can't Post

What error are you receiving when you try to run the code I provided? I tested the code thoroughly prior to posting it, so I know it works.

Could you please post your code so we can test it?


Rivotti
User

Apr 10, 2000, 9:28 AM

Post #5 of 11 (1944 views)
Re: How to generate a word with 8 random characters? [In reply to] Can't Post

Is strange, I think nothing is wrong...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl
use CGI qw( :standard );

print "Content-type: text/html\n\n";

...

@chars=("A".."Z","a".."z","0".."9");
for ($i=1; $i<=8;$i++)
{ $random .= $salt[rand @chars]; }
print "$random\n";

...</pre><HR></BLOCKQUOTE>
I run the program and does nothing.

Another thing: I've posted "Canīt Upload File" some days ago, and I still have problems. Can you see that post? It would be nice if you could.

Thanks again for you help Jasmine
Rivotti


Jasmine
Administrator / Moderator

Apr 10, 2000, 9:40 AM

Post #6 of 11 (1944 views)
Re: How to generate a word with 8 random characters? [In reply to] Can't Post

My bad Frown I posted a booboo

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@chars=("A".."Z","a".."z","0".."9");

for ($i=1; $i<=8;$i++) {
$random .= $chars[rand @chars];
}

print "$random\n";
</pre><HR></BLOCKQUOTE>

Just change the $salt in my previous post to $chars

Sorry about the confusion.

I'll take a look at your other post later.


Rivotti
User

Apr 10, 2000, 9:45 AM

Post #7 of 11 (1944 views)
Re: How to generate a word with 8 random characters? [In reply to] Can't Post

Hey, that was fast Smile

Now it makes sense. Thank you again you're great

Rivotti


Rivotti
User

Apr 14, 2000, 1:34 AM

Post #8 of 11 (1944 views)
Re: How to generate a word with 8 random characters? [In reply to] Can't Post

Thanks Cure:

It's allways good to know diferent ways to do something. Good lesson. Smile

Rivotti

P.S. - I Have 2 replys without any answer in the forum. Can you check it out? Thanks in advance.


Cure
User

Apr 14, 2000, 10:48 AM

Post #9 of 11 (1944 views)
Re: How to generate a word with 8 random characters? [In reply to] Can't Post

Hi

@chars = ( 'A'..'Z', 'a'..'z', 0..9 );
$len = 8;
$pwd .= $chars[rand @chars] while length $pwd < $len;
print "$pwd\n";


Cure


Cure
User

Apr 14, 2000, 1:02 PM

Post #10 of 11 (1944 views)
Re: How to generate a word with 8 random characters? [In reply to] Can't Post

Hi

my $test = [ 'A'..'Z', 'a'..'z', 0..9 ];
my $pwd = "";
map { $pwd .= $_} ($test->[rand @$test]), 2..8;
print $pwd;


Cure

[This message has been edited by Cure (edited 04-14-2000).]


Rivotti
User

Apr 14, 2000, 2:45 PM

Post #11 of 11 (1944 views)
Re: How to generate a word with 8 random characters? [In reply to] Can't Post

Ok, Ok...

I got the picture. Thanks... Smile

Rivotti

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives