RE: Randomly selecting characters from a list
Actually that's probably about as good as any other way. Quoting from the Perl Cookbook: "Making random numbers is hard." Also from the Perl Cookbook, here is a way to generate a 10 character string 'randomly': @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9 ); $password = join( "", @chars[ map { rand @chars } ( 1 .. 10 ) ] ); print "$password \n"; Regards, Rick ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: Randomly selecting characters from a list
You could try: use strict; my @chars = split //, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; my $out; rand (time); # or some better seed # allow duplicate characters for (1..10) { $out .= $chars[int (rand scalar @chars)]; } # unique characters for (1..10) { my $idx = int (rand scalar @chars); $out .= splice @chars, $idx, 1; } print $out; Peter Guzis Web Administrator, Sr. ENCAD, Inc. email: [EMAIL PROTECTED] www.encad.com -Original Message- From: Roee Rubin [mailto:[EMAIL PROTECTED]] Sent: Monday, June 25, 2001 4:08 PM To: Perl-Win32-Users Subject: Randomly selecting characters from a list Hello, I need to randomly select 10 characters from a character list and am having some trouble. One way I thought of approaching this is getting a random number (1-38) and selecting the character in that location of the string. There must be better ways of approaching this. Any help would be appreciated. list of characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 Thanks! Roee Rubin Irubin Consulting [EMAIL PROTECTED] http://www.irubin.com/ ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Re: Randomly selecting characters from a list
At 04:08 PM 2001.06.25 -0700, Roee Rubin wrote: >I need to randomly select 10 characters Can the characters repeat? If so then it's easy -- just select one from the set randomly ten times. If they have to be unique -- that is, if you need to have a random sequence or permutation -- then it's a little bit trickier. You can probably do it desctructively, by extracting items from the list and then contracting what remains, but I'm not sure if that's even on the right track. Alternatively, you can just go with plan A, select randomly as many times as necessary until you have 10 unique values. I suspect this will typically loop many more than 10 times, but I don't think it would ever run *that* long. -- Chris Devers [EMAIL PROTECTED] ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users