Solved

Random password generation

Posted on 1998-12-13
3
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How does Redux fit into React? 3 92
Need script to search multiple files in one drive 13 58
PowerShell and cisco ios 3 46
Ruby or Python 7 61
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to dynamically set the form action using jQuery.
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…

733 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