Solved

Random password generation

Posted on 1998-12-13
3
157 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

910 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

21 Experts available now in Live!

Get 1:1 Help Now