Solved

Random password generation

Posted on 1998-12-13
3
156 Views
Last Modified: 2013-12-25
How do I generate a random password with 6 letters that vary in upper and lower case? I tried this code:

for ($numdigits = 1; $numdigits <= 6; $numdigits++) {
  srand(time ^ $$);
  @letters = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
  $randnumber = rand(@letters);
  $randpassword .= $letters[$randnumber]
}

Since the seed is the time, it just generates the same random letter six times. Also, there must be a better way to do it than having an array with all of the letters...
0
Comment
Question by:chrisbolt
3 Comments
 

Accepted Solution

by:
Gunnar earned 50 total points
ID: 1830102
You should not call srand for each iteration. Call it only once, when your program starts. If you use Perl 5.004 or later, it is not necessary to call srand() at all, as it is done automatically. (Actually, it was done automatically in older versions too, but with a bad seed.)

To avoid the letter array, generate an ASCII code between 65 and 80 (A..Z). Then, flip a coin (that is, check if rand() > 0.5); if so, add 32 (which transfers the code to the range 97..112, a..z). Example:

srand(time ^ $$);

for ($n = 1; $n <= 6; $n++) {
  $ch = 65+rand(26);
  if (rand() > 0.5) {
    $ch += 32;
  }
  $pw .= chr($ch);
}

0
 
LVL 2

Author Comment

by:chrisbolt
ID: 1830103
Thank you, worked like a charm!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1830104
srand(time + $$) may be less likely to repeat when both time and $$ increase by one.
(or in 5.004 or later, just omit the srand, as Gunnar sugests)
seeding srand this way may also make it too easy for someone to guess what password was generated. especially if they can get a general idea of when it was generated.
And if you generate a password for one person, they may be able to figure out your clock setting by trying various seeds until they generate the same passord,
and then have a good guess about what passwords you may be generating for others.

you can also say
@letters = ('a'..'z','A'..'Z');
to initialize the array (just once, not for each itteration)
or
 $randpassword = join'',@letters[map{rand @letters}(1)x6];
to generate 6 random letters
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now